Bitwise operation

From Simple English Wikipedia, the free encyclopedia
Jump to navigation Jump to search

In computer programming, a bitwise operation operates on a bit array at the level of its individual bits.

Bitwise operators[change | change source]

In the explanations below, any indication of a bit's position is counted from the right side, moving left. For example, the binary value 0001 (decimal 1) has zeroes at every position but the first (i.e. the rightmost) one.

NOT[change | change source]

The bitwise NOT, or complement, is an operation that performs logical negation on each bit. Bits that are 0 become 1, and those that are 1 become 0. For example:

NOT 0111  (decimal 7)
  = 1000  (decimal 8)
NOT 10101011  (decimal 171)
  = 01010100  (decimal 84)

AND[change | change source]

A bitwise AND is an operation that takes two bit patterns and performs the logical AND operation on each pair of bits. If both bits in the compared position are 1, the result is 1 otherwise the result is 0. For example:

    0101 (decimal 5)
AND 0011 (decimal 3)
  = 0001 (decimal 1)

OR[change | change source]

A bitwise OR is an operation that takes two bit patterns of equal length and performs the logical inclusive OR operation on each pair of bits. If if both bits are 0 the result is 0, while otherwise, the result is 1. For example:

   0101 (decimal 5)
OR 0011 (decimal 3)
 = 0111 (decimal 7)

XOR[change | change source]

A bitwise XOR is an operation that takes two bit patterns of equal length and performs the logical exclusive OR operation on each pair of bits. If both bits are 0 the result 0, if one bit is 1 and the other is zero the result is 1, and if both bits are 1 the result is 0.

    0101 (decimal 5)
XOR 0011 (decimal 3)
  = 0110 (decimal 6)