chess:programming:bit_operations
Differences
This shows you the differences between two versions of the page.
Both sides previous revisionPrevious revisionNext revision | Previous revision | ||
chess:programming:bit_operations [2021/11/03 11:46] – peter | chess:programming:bit_operations [2021/11/05 09:55] (current) – peter | ||
---|---|---|---|
Line 9: | Line 9: | ||
|x >> y|Shift the bits of x to the right y positions|25 >> 3 = 10101 >> 3 = 00010 = 2| | |x >> y|Shift the bits of x to the right y positions|25 >> 3 = 10101 >> 3 = 00010 = 2| | ||
+ | ---- | ||
+ | |||
+ | ===== Get only even or odd bits ===== | ||
+ | |||
+ | Take a number, say 12345678. | ||
+ | |||
+ | * In Binary, this is 00000000101111000110000101001110. | ||
+ | |||
+ | Apply an even mask against it: | ||
+ | |||
+ | < | ||
+ | 00000000101111000110000101001110 | ||
+ | 10101010101010101010101010101010 | ||
+ | ———————————————————————————————— | ||
+ | 00000000101010000010000000001010 | ||
+ | </ | ||
+ | |||
+ | Apply an odd mask against it: | ||
+ | |||
+ | < | ||
+ | 00101101011001010111000110001001 | ||
+ | 01010101010101010101010101010101 | ||
+ | ———————————————————————————————— | ||
+ | 00000000000101000100000101000100 | ||
+ | </ | ||
+ | |||
+ | <WRAP info> | ||
+ | **NOTE: | ||
+ | |||
+ | * Mask **0xAAAAAAAA** has all its even bits set. | ||
+ | * Its bitwise AND with a number will separate out the even bits. | ||
+ | * 1010 1010 1010 1010 1010 1010 1010 1010 | ||
+ | |||
+ | * Mask **0x55555555** has all its odd bits set. | ||
+ | * Its bitwise AND with a number will separate out the odd bits. | ||
+ | * 1010 1010 1010 1010 1010 1010 1010 1010 | ||
+ | |||
+ | </ | ||
+ | |||
+ | ---- | ||
chess/programming/bit_operations.1635939991.txt.gz · Last modified: 2021/11/03 11:46 by peter