User Tools

Site Tools


chess:programming:bit_operations

Differences

This shows you the differences between two versions of the page.

Link to this comparison view

Both sides previous revisionPrevious revision
Next revision
Previous revision
chess:programming:bit_operations [2021/11/03 11:45] peterchess:programming:bit_operations [2021/11/05 09:55] (current) peter
Line 3: Line 3:
 ^Expression^Description^Example^ ^Expression^Description^Example^
 |x & y|AND the bits of x with those of y|21 & 7 = 10101 & 00111 = 00101 = 5| |x & y|AND the bits of x with those of y|21 & 7 = 10101 & 00111 = 00101 = 5|
-|x <nowiki>|</nowiki> y|OR the bits of x with those of y|21 | 7 = 10101 | 00111 = 10111 = 23| +|x <nowiki>|</nowiki> y|OR the bits of x with those of y|21 <nowiki>|</nowiki> 7 = 10101 <nowiki>|</nowiki> 00111 = 10111 = 23| 
-|x <nowiki>^</nowiki> y|XOR the bits of x with those of y|21 ^ 7 = 10101 ^ 00111 = 10010 = 18|+|x <nowiki>^</nowiki> y|XOR the bits of x with those of y|21 <nowiki>^</nowiki> 7 = 10101 <nowiki>^</nowiki> 00111 = 10010 = 18|
 |~x|Inverting (complementing) the bits of x (0 -> 1 and 1 -> 0)|~7 = 11111111111111000 = 65528| |~x|Inverting (complementing) the bits of x (0 -> 1 and 1 -> 0)|~7 = 11111111111111000 = 65528|
 |x << y|Shift the bits of x to the left y positions|25 << 3 = 10101 << 3 = 10101000 = 168| |x << y|Shift the bits of x to the left y positions|25 << 3 = 10101 << 3 = 10101000 = 168|
 |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:
 +
 +<code>
 +00000000101111000110000101001110   &    (n)
 +10101010101010101010101010101010        (0xAAAAAAAA)
 +————————————————————————————————
 +00000000101010000010000000001010        (Contains all even bits)
 +</code> 
 +
 +Apply an odd mask against it:
 +
 +<code>
 +00101101011001010111000110001001   &    (n)
 +01010101010101010101010101010101        (0x55555555)
 +————————————————————————————————
 +00000000000101000100000101000100        (Contains all odd bits)
 +</code> 
 +
 +<WRAP info>
 +**NOTE:**  Use Masks.
 +
 +  * 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
 +
 +</WRAP>
 +
 +----
  
chess/programming/bit_operations.1635939949.txt.gz · Last modified: 2021/11/03 11:45 by peter

Donate Powered by PHP Valid HTML5 Valid CSS Driven by DokuWiki