Home>

What is a bit operation

Bit operators operate on binary,These operators can only be used for operations of integer type.Such as:char, short, int, long

Obtaining high and low values ​​by bit operators

int a=0x1234;

int high, low;

high=(a>>8)&0x00ff;

low=a&0x00ff;

Left shift operator and right shift operator (<<>)

Left shift is a binary number,Move several bits,The vacated position on the right is filled with 0, and the high-order left shift overflow should discard the high-order bit.

For example:inta=8, a=00001000;

a<2 then a=00100000;so the value of a is a * 2 ^ 2=32;

During the left shift,If the high-order bits of the number discarded by overflow do not contain 1, the value after shifting is the value * 2 ^ number of bits moved.

Shift right is a binary number,Shift right by several bits,The left vacant position is filled with 0 (if it is an unsigned type,If it is a signed type, it is completed according to the regulations of the operating system.Some operating systems are filled with sign bits,(Some operating systems default to 0.)

XOR (^)

The two values ​​participating in the binary operation are the same as 0, otherwise they are 1

1. XOR with 0, keep the original value

2. Swap the two values,No temporary variables

a=3, b=4

a=a ^ b;

b=b ^ a;(b=b ^ a ^ b)

a=a ^ b;(a ^ b ^ a ^ b ^ b)

c
  • Previous Shell script to delete mail queue
  • Next Method for implementing 360-degree flip special effect of button control picture by js