Created
October 8, 2017 08:15
-
-
Save kaityo256/2e153deb8d9b0dffb01c0dc1ed6d91d9 to your computer and use it in GitHub Desktop.
Find the left-most bit
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #include <cstdio> | |
| #include <cstdint> | |
| #include <cstdlib> | |
| void | |
| show_bit(uint32_t v) { | |
| for (int i = 0; i < 32; i++) { | |
| printf("%d", (v & (1ll << (31 - i)) ? 1 : 0)); | |
| } | |
| printf("\n"); | |
| } | |
| uint32_t | |
| lmb(uint32_t v) { | |
| if (v == 0)return 0; | |
| else { | |
| return 1 << (31 ^ __builtin_clz(v)); | |
| } | |
| } | |
| int | |
| main(void) { | |
| for (uint32_t i = 0; i < 5; i++) { | |
| int v = rand(); | |
| printf("\n"); | |
| show_bit(v); | |
| show_bit(lmb(v)); | |
| } | |
| } |
Author
kaityo256
commented
Oct 8, 2017
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment