C++ Bitwise Operations (Ninety-Three) [Part 1]

Long time no see! Due to personal and academic issues, I stopped updating for six months. Today, I will continue to publish articles, and I hope everyone enjoys them.Today, we will learn about: bitwise operations1. Brief Overview of Bitwise OperatorsBitwise operations are a type of operator. When we use a computer, it cannot understand our numbers directly and converts them into binary, represented by 0s and 1s. Generally, after computation, the result is a string of 0s and 1s, which the computer converts back to decimal for us to understand.For example:

int a = 19; int b = 23; cout << a + b << endl; Calculation process:  10011  10111 101010 Result: 52

This is the basic principle of computer calculations.In addition to the four basic operations +, -, *, and /, there are many other bitwise operations.2. DepthUnderstanding Other Advanced Operators

& AND The result is 1 only when both bits are 1
| OR The result is 0 only when both bits are 0
^ XOR Bits that are the same yield 0, different yield 1
~ NOT 0 becomes 1, 1 becomes 0
<< Left Shift All bits shift left by a certain number of positions, high bits are discarded, low bits are filled with 0
>> Right Shift All bits shift right by a certain number of positions, high bits are filled with 0 or sign bits are filled

(1) Bitwise AND OperatorOperation rules:

0 & 0 = 0 0 & 1 = 0 1 & 0 = 0 1 & 1 = 1

Summary: The result is 1 only when both bits are 1; otherwise, the result is 0.

For example, with 19 and 23:

19=10011 23=10111 Counting from the right: First bit: 1 & 1 = 1 Second bit: 1 & 1 = 1 Third bit: 0 & 1 = 0 Fourth bit: 0 & 0 = 0 Fifth bit: 1 & 1 = 1 The resulting number: 10011 which equals: 19
  1. Zeroing: To clear a unit, simply AND it with a number where all bits are zero, and the result will be zero.
  2. Getting Specific Bits of a Number: For example, to get the lower 4 bits of the number <span>X = 1010 1110</span>, you just need another number <span>Y = <span>0000 </span>1111</span>, then <span>X & Y = 0000 1110</span> to obtain the specific bits of <span>X</span>.
  3. Determining Odd or Even: By checking whether the last bit is 0 or 1, you can determine odd or even. You can use <span><span>if ((a & 1) == 0)</span></span> instead of <span><span>if (a % 2 == 0)</span></span> to check if <span>a</span> is even.

(2) Bitwise OR Operator

Operation rules:

0 | 0 = 0 0 | 1 = 1 1 | 0 = 1 1 | 1 = 1

Summary: The result is 1 if at least one of the bits is 1; the result is 0 only if both are 0.

Again, with 19 and 23:

19=10011 23=10111 Counting from the right: First bit: 1 | 1 = 1 Second bit: 1 | 1 = 1 Third bit: 0 | 1 = 1 Fourth bit: 0 | 0 = 0 Fifth bit: 1 | 1 = 1 The resulting number: 10111 which equals: 23
  1. Setting Certain Bits to 1: For example, to set the lower 4 bits of the number <span>X = 1010 1110</span> to 1, you just need another number <span>Y = 0000<span> 1111</span></span>, then <span><span>X | Y</span> = 1010 1111</span> to obtain the result.

(3) XOR OperatorOperation rules:

0 ^ 0 = 0 0 ^ 1 = 1 1 ^ 0 = 1 1 ^ 1 = 0

Summary: The result is 0 when the corresponding bits are the same, and 1 when they are different.

Properties:

  1. Commutative Law
  2. Associative Law: <span>(a ^ b) ^ c == a ^ (b ^ c)</span>
  3. For any number <span>x</span>, it holds that <span>x ^ x = 0</span>, <span>x ^ 0 = x</span>
  4. Reflexivity:<span>a ^ b ^ b = a ^ 0 = a</span>

Uses:

  1. Flipping Specific Bits: For example, to flip the lower 4 bits of the number <span>X = 1010 1110</span>, you just need another number <span>Y = <span>0000 1111</span></span>, then <span><span>X ^ Y</span> = 1010 0001</span> to obtain the result.
  2. XOR with 0 leaves the value unchanged: For example <span>1010 1110 ^ 0000 0000 = 1010 1110</span>

Next time, we will continue to explore NOT, left shift, right shift, and other bitwise operators.

Leave a Comment