Skip to content
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

Day 248 #512

Closed
vaskoz opened this issue Apr 27, 2019 · 3 comments · Fixed by #513
Closed

Day 248 #512

vaskoz opened this issue Apr 27, 2019 · 3 comments · Fixed by #513
Assignees

Comments

@vaskoz
Copy link
Owner

vaskoz commented Apr 27, 2019

Good morning! Here's your coding interview problem for today.

This problem was asked by Nvidia.

Find the maximum of two numbers without using any if-else statements, branching, or direct comparisons.

@vaskoz vaskoz self-assigned this Apr 27, 2019
@vaskoz
Copy link
Owner Author

vaskoz commented Apr 27, 2019

A bit-fiddling question.

@vaskoz
Copy link
Owner Author

vaskoz commented Apr 27, 2019

I used this article to understand the math behind the bit manipulation: https://www.geeksforgeeks.org/compute-the-minimum-or-maximum-max-of-two-integers-without-branching/

NOTE: The XOR version can't be done without branching in Go since Go doesn't provide integer values for Boolean values.

@vaskoz
Copy link
Owner Author

vaskoz commented Apr 27, 2019

goos: darwin
goarch: amd64
BenchmarkMaxIf-4                 	 2000000	       906 ns/op
BenchmarkMaxSubtractAndShift-4   	 3000000	       494 ns/op
BenchmarkMaxXor-4                	 2000000	      1027 ns/op

@vaskoz vaskoz mentioned this issue Apr 27, 2019
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

Successfully merging a pull request may close this issue.

1 participant