Bitwise Operators

Helpful script writing tricks and HowTo's
User avatar
jNizM
Posts: 3201
Joined: 30 Sep 2013, 01:33
Contact:

Bitwise Operators

Post by jNizM » 28 Jan 2014, 08:31

Bitwise Operators

Code: Select all

&    -  bitwise and
|    -  bitwise or
^    -  bitwise xor
~    -  bitwise not
<<   -  bitwise shift left
>>   -  bitwise shift right

Bitwise AND [BitAnd] &

Code: Select all

  HEX   | Bits               | Description
--------┼--------------------┼----------------
  0x99  | 1 0 0 1   1 0 0 1  | the Value
   &    |         &          | Bitwise AND
  0x3C  | 0 0 1 1   1 1 0 0  | the Mask
   =    |         =          |
  0x18  | 0 0 0 1   1 0 0 0  |
--------┴--------------------┴----------------


MsgBox, % 0x99 & 0x3C   ;output: 0x18 (24)

Bitwise OR [BitOr] |

Code: Select all

  HEX   | Bits               | Description
--------┼--------------------┼----------------
  0x99  | 1 0 0 1   1 0 0 1  | the Value
   |    |         |          | Bitwise OR
  0x3C  | 0 0 1 1   1 1 0 0  | the Mask
   =    |         =          |
  0xDB  | 1 0 1 1   1 1 0 1  |
--------┴--------------------┴----------------


MsgBox, % 0x99 | 0x3C   ;output: 0xDB (219)

Bitwise EXCLUSIVE OR [BitXor] ^

Code: Select all

  HEX   | Bits               | Description
--------┼--------------------┼----------------
  0x99  | 1 0 0 1   1 0 0 1  | the Value
   ^    |         ^          | Bitwise EXCLUSIVE OR
  0x3C  | 0 0 1 1   1 1 0 0  | the Mask
   =    |         =          |
  0xA5  | 1 0 1 0   0 1 0 1  |
--------┴--------------------┴----------------


MsgBox, % 0x99 ^ 0x3C   ;output: 0xA5 (165)

Bitwise NOT [BitNot] ~

Code: Select all

  HEX   | Bits
--------┼--------------------
~ 0x99  | 1 0 0 1   1 0 0 1
   =    |
  0x66  | 0 1 1 0   0 1 1 0
--------┴--------------------

MsgBox, % ~ 0x99   ;output: 0x66 (102)

Bitwise SHIFT LEFT [BitShiftLeft] <<

Code: Select all

  HEX   | Bits
--------┼--------------------
  0x03  | 0 0 0 0   0 0 1 1
   <<   |         <<
= 0x06  | 0 0 0 0   0 1 1 0
--------┴--------------------

  HEX   | Bits
--------┼--------------------
  0xC0  | 1 1 0 0   0 0 0 0
   <<   |         <<
= 0x80  | 1 0 0 0   0 0 0 0
--------┴--------------------

Bitwise SHIFT RIGHT [BitShiftRight] >>

Code: Select all

  HEX   | Bits
--------┼--------------------
  0x06  | 0 0 0 0   0 1 1 0
   >>   |         >>
= 0x03  | 0 0 0 0   0 0 1 1
--------┴--------------------

  HEX   | Bits
--------┼--------------------
  0x03  | 0 0 0 0   0 0 1 1
   >>   |         >>
= 0x01  | 0 0 0 0   0 0 0 1
--------┴--------------------

Bitwise ROTATE LEFT

Code: Select all

  HEX   | Bits
--------┼--------------------
  0x03  | 1 1 0 0   0 0 0 0
   RoL  |        RoL
= 0x81  | 1 0 0 0   0 0 0 1
--------┴--------------------

Bitwise ROTATE RIGHT

Code: Select all

  HEX   | Bits
--------┼--------------------
  0xC0  | 0 0 0 0   0 0 1 1
   RoR  |        RoR
= 0x81  | 1 0 0 0   0 0 0 1
--------┴--------------------

Useful Links:
- Wikipedia - Bitwise operation
- C++ Tutorial: Bitwise Operators
- Low Level Bit Hacks You Absolutely Must Know
- bithacks.h - bit hacks header file
- Understanding Bitwise Operators


Todo:
- more explanations
- more examples
- |= &= ^= >>= <<=
- ...

BitRotate
a << b | (a & 0xFFFFFFFF) >> (32-b)
Last edited by jNizM on 17 Mar 2014, 06:16, edited 11 times in total.
[AHK] v2.0.18 | [WIN] 11 Pro (23H2) | [GitHub] Profile
User avatar
nnnik
Posts: 4500
Joined: 30 Sep 2013, 01:01
Location: Germany

Re: Bitwise Operators

Post by nnnik » 28 Jan 2014, 15:56

a^b=(a&b)^(a|b)
Recommends AHK Studio
Post Reply

Return to “Tutorials (v1)”