Skip to content

Petty Suggestion #1

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
Mark-Maplarge opened this issue Aug 7, 2024 · 3 comments
Open

Petty Suggestion #1

Mark-Maplarge opened this issue Aug 7, 2024 · 3 comments

Comments

@Mark-Maplarge
Copy link

This code:

int checkLength = k % 2 == 0 ? k / 2 : k / 2 + 1;

can instead be:

int checkLength = (k + 1) % 2;

This saves a branch.

@epsitec
Copy link

epsitec commented Apr 6, 2025

Your suggestion does not produce the same result.

Given k=10, the initial checkLength would be k/2 = 5, whereas it would be 11%2 = 1 in your suggestion...

I suppose you meant something along the lines of:

int checkLength = (k / 2) + (k % 2);

which does indeed save a branch 😃

@Mark-Maplarge
Copy link
Author

My bad I meant (k + 1) / 2

@epsitec
Copy link

epsitec commented Apr 7, 2025

Of course 🤣 . I feel stupid.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants