A rule for classical bit operations

1. In the program, we often use one field to record multiple simple types. For example, when the administrator manages posts, a post has multiple attributes such as top, hot discussion, and boutique. These attributes are relatively simple, like the top state only has two states: top and untop; if each field adds a field to the database, if there are many states, it is very redundant; we can pass these attributes through bit operations (a set of special values) stored in a field in a database. We use this set of values ​​to represent one of our states (such as 1. Top 2. Untop). The reason why it is called a special set of values ​​is that the value of these stored states must be 2 n.

2. What is bit operation?

Bit operation is the operation of binary values ​​according to the bit operators & (and), | (or), ~ (inversion), ^ (exclusive or), >> (left shift), << (right shift), We know that all numbers in a program are stored in binary form in computer memory. Bit operation, to put it bluntly, is to operate directly on the binary bits of integers in memory.

For example, the and operation is originally a logical operator, but the and operation can also be performed between integers. For example, the binary of 6 is 110, and the binary of 11 is 1011, then the result of 6 and 11 is 2, which is the result of the logical operation of the corresponding bits in binary (0 means False, 1 means True, and the empty bits are treated as 0) .

3. How are binary and decimal converted to each other?

 Conversion between decimal and binary: Converting a binary number to decimal is to multiply each number in binary by the corresponding power of 2 from right to left (the power should start from 0);

For example: 1101(2)=1*2 0 +0*2 1 +1*2 2 +1*2 3 =1+0+4+8=13;

4. Binary operators and usage rules:

bitwise symbols

name

rule

example

&

AND operator

If both numbers in the same bit are 1, it is 1; if one of them is not 1, it is 0.

1 & 1 =1;0 & 0=0

1 & 0 = 0 ;0 & 1=0

|

or operator

The same bit is 1 as long as one is 1.

 

1 | 1=1 ;0|0=0

1 |0=1;0|1=1

^

XOR operator

If the same bit is different, it is 1, and the same bit is 0.

1 ^1=0 ;0^0=0

1 ^0=1;0^1=1

~

negation operator

0 and 1 are all negated.

~1=0;~0=1

<< 

left shift operation

The vacant bit on the right is filled with 0, and the upper bit is discarded when the upper bit is shifted to the left and overflows.

 

>> 

right shift operation

The vacant bits on the left are filled with 0 or 1. Positive numbers are padded with 0, and negative numbers are padded with 1. If the low bit is shifted to the right and overflows, the bit is discarded.

 

     4. The binary value corresponding to the decimal number 2 n , carefully study this set of data and you will find the mystery:

binary value

00000001

00000010

00000100

00001000

00010000

00100000

01000000

10000000

100000000

decimal value

20 =1

21 =2

22=4

23=8

24=16

25=32

26=64

27=128

28=256

     Through careful observation and research we find the decimal numbers 1, 2, 4, 8, 16, 32, 64....2 n   . There is a rule that they are converted into binary for bit operations. Let's understand this rule through an example.

     管理员在管理我们帖子的时候,可以对我们的帖子进行置顶,热议,锁定等操作,我们在数据库中用一个字段Status来记录这些操作,我们可以创建一些枚举值来表示这些操作状态 1 :锁定 2:置顶  4:热议 8:其他状态 。。。。。。。

          (1)一个帖子同时是热议和置顶的,那么它的Status=2|4=2+4=6  数据库Status中保留的就是6,若是再锁定,Status=2|4|1=7 数据库Status中保留的就是7。

          (2)取消置顶Status= Status&(~2)=6&(~2)=6-2=4(相当于减法)

            (3)判断这个帖子是否置顶 Status&2 =6&2=2就说明置顶状态(2)包含在6中;

(1)添加元素:(由于这一组数字都是2n,他们转化为二进制都只是在所在的位上,所以通过或运算实际上就是这两个数相加)

 

|运算符

 

1|2

00001|00010=00011

20|21=20+21=3

1|4

00001|00100=00101

20|22=20+22=5

2|8

00010|01000=01010

21|23=21+23=10

1|2|8

00001|00010|01000=01010

20|21|23=20+21+23=11

                           (2)判断一个元素是否存在:(要判断的那个数若是包含在Status(通过或运算得出来的值)中通过与运算只能得到自己的值)

&十进制运算

&二进制运算

结果

10&8

1010&1000=1000

8

10&2

1010&10=0010

2

11&8

1011&1000=1000

8

                         (3)移除一个元素:(通过取反运算得到自己数的反数,然后再进行与运算就把这个数个移除了)

十进制

二进制

十进制结果

10&(~8)

1010&(~1000)=0010

2

10&(~2)

1010&(~0010)=1000

8

11&(~8)

1011&(~1000)=0011

3


Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325742433&siteId=291194637