Last active
August 27, 2017 15:21
-
-
Save singhsays/d8696b51b95251eb04188afd547664d0 to your computer and use it in GitHub Desktop.
bithacks
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
a.1 = a | |
a.0 = 0 | |
a+1 = 1 | |
a+0 = a | |
a^1 = ā | |
a^0 = a | |
~-a = a-1 | |
-a = ~a+1 // Two's complement | |
unset rightmost set bit: a & (a-1) | |
set rightmost unset bit: a | (a+1) | |
get rightmost set bit: a & -a , also a^(a&(a-1)) | |
clear trailing bits: a & (a+1) | |
set trailing bits: a | (a-1) | |
get ith bit: a & (1u << i) | |
unset ith bit: a & ~(1u << i) | |
set ith bit: a | (1u << i) | |
a&(a+1) | |
a&(a-1) | |
useful masks: | |
0x1 = 0b00000000000000000000000000000001 = 1 | |
0xFFFFFFFF = 0b11111111111111111111111111111111 = -1 | |
0xAAAAAAAA = 0b10101010101010101010101010101010 = 2863311530 | |
0x55555555 = 0b01010101010101010101010101010101 = 1431655765 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment