Easy Tutorial
❮ Android Tutorial Listener Edittext Change Linux Common Command 2 ❯

Bitwise Operations (&, |, ^, ~, >>, <<)

Category Programming Techniques

1. Overview of Bitwise Operations

All data in modern computers is stored in binary form, which consists of two states, 0 and 1. The operations that computers perform on binary data (+, -, *, /) are called bitwise operations, which means that the sign bit is also involved in the computation.

Let's illustrate this with a simple example to see how the CPU performs calculations. For instance, consider the following line of code:

int a = 35;
int b = 47;
int c = a + b;

To calculate the sum of two numbers, since all operations are performed in binary within the computer, the integer variables we provided above will first be converted to binary and then added:

35:  0 0 1 0 0 0 1 1
47:  0 0 1 0 1 1 1 1
---------------------------------
82:  0 1 0 1 0 0 1 0

Therefore, compared to directly using the (+, -, *, /) operators in the code, the proper use of bitwise operations can significantly improve the execution efficiency of the code on the machine.

2. Bitwise Operation Overview

Symbol Description Operation Rules
& AND The result is 1 only if both bits are 1
OR The result is 0 only if both bits are 0
^ XOR The result is 0 if the bits are the same, and 1 if they are different
~ NOT 0 becomes 1, and 1 becomes 0
<< Left Shift All binary positions are shifted to the left by a certain number of positions, discarding the high positions and filling the low positions with 0
>> Right Shift All binary positions are shifted to the right by a certain number of positions, with the high positions filled with 0 for unsigned numbers, and different compilers handle signed numbers differently, some fill with the sign bit (arithmetic right shift), and some fill with 0 (logical right shift)

3. Bitwise AND Operator (&)

Definition: The two data participating in the operation perform an "AND" operation on the binary positions.

Operation Rules:

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

Summary: The result is 1 only if both positions are 1, otherwise the result is 0.

Example: 3&5, which is 0000 0011 & 0000 0101 = 0000 0001, so the value of 3&5 is 1.

Note: Negative numbers participate in bitwise AND operations in their two's complement form.

Uses of AND operation:

1) Clearing to Zero

If you want to clear a unit to zero, making all its binary positions 0, just AND it with a number that has all positions zero, and the result will be zero.

2) Taking a Specific Position of a Number

For example, to take the low 4 bits of the number X=1010 1110, just find another number Y, let the low 4 bits of Y be 1, and the rest be 0, i.e., Y=0000 1111, then perform a bitwise AND operation with X (X&Y=0000 1110) to get the specific position of X.

3) Judging Odd or Even

Just decide based on whether the last bit is 0 or 1, 0 means even, and 1 means odd. Therefore, you can use if ((a & 1) == 0) instead of if (a % 2 == 0) to determine if a is even.

  1. Bitwise OR Operator (|)

Definition: The two objects participating in the operation perform an "OR" operation on the binary positions.

Operation Rules:

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

Summary: As long as one of the two objects participating in the operation is 1, its value is 1.

Example: 3|5, which is 0000 0011 | 0000 0101 = 0000 0111, so the value of 3|5 is 7.

Note: Negative numbers participate in bitwise OR operations in their two's complement form.

Uses of OR operation:

1) Commonly used to set

❮ Android Tutorial Listener Edittext Change Linux Common Command 2 ❯