当前位置:网站首页>Bitwise Operators

Bitwise Operators

2022-06-24 16:16:00 HLee

brief introduction

All data from modern computers are stored in binary form in the device . namely 0、1 Two kinds of state , The operation of a computer on binary data (+、-、*、/) It's all called bit operations , That is, the operation in which symbol bits participate in the operation .

Words alone are no proof , Let's take a simple example CPU How is it calculated , Like this line of code :

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

Calculate the sum of two numbers , Because in the computer are binary to carry out operations , So what we gave above int Variables will be converted to binary first inside the machine 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 using it directly in code (+、-、*、/) Operator , Reasonable use of bit operation can significantly improve the efficiency of code execution on the machine .

Symbol

describe

Operational rules

&

And

Both of them are 1 when , The result is 1

|

or

Both of them are 0 when , The result is 0

^

Exclusive or

The two bits are the same 0, Dissimilarity is 1

~

Take the opposite

0 change 1,1 change 0

<<

Move left

Each binary moves several bits to the left , High level discard , Low complement 0

>>

Move right

Each binary moves several bits to the right , To an unsigned number , High compensation 0, Signed number , Different compiler processes , Some complement sign bits ( Arithmetic shift right ), Some supplements 0( Logical shift right )

Bitwise and operator (&)

Definition : The two data involved in the calculation , By binary bit " And " operation .

Operational rules : Only the binary of two numbers is 1, The result is 1, Otherwise 0.( Negative numbers take part in the bitwise sum operation in the form of complement )

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

 example :3 & 5   namely  00000011 & 00000101 = 00000001 , therefore  3 & 5 The value of is 1.

 Be careful : Negative numbers take part in the bitwise sum operation in the form of complement .

And the use of computation :

1) Zero clearing

If you want to clear a unit , Even if all its binary bits are 0, As long as it matches a value where everyone is zero , The result is zero .

2) Take a number of fingers to locate

For example, take the number X=1010 1110 It's low 4 position , Just find another number Y, Make Y It's low 4 Position as 1, The rest are 0, namely Y=0000 1111, And then X And Y To perform bitwise operations (X&Y=0000 1110) You can get X Point positioning of .

3) Judge parity

As long as the bottom line is 0 still 1 To decide , by 0 Even numbers , by 1 It's an odd number . So you can use if ((a & 1) == 0) Instead of if (a % 2 == 0) To judge a Is it an even number .

if ((2 & 1) == 0) {
    System.out.println(" even numbers ");
} else {
    System.out.println(" Odd number ");
}

bitwise or operator (|)

Definition : Two objects involved in the operation , By binary bit " or " operation .

Operational rules : The two numbers participating in the operation as long as one of the two numbers is 1, The results for 1.

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

 example :2 | 4  namely  00000010 | 00000100 = 00000110 , therefore 2 | 4 The value of is  6 .

 Be careful : A negative number participates in a bitwise OR operation in the form of a complement .

Or the use of arithmetic :

1) Often used to set some bits of a data to 1

Like counting X=1010 1110 It's low 4 Bit is set to 1, Just find another number Y, Make Y It's low 4 Position as 1, The rest are 0, namely Y=0000 1111, And then X And Y To perform a bitwise or arithmetic operation (X | Y=1010 1111) You can get .

Exclusive or operator (^)

Definition : The two data involved in the calculation , By binary bit " Exclusive or " operation .

Operational rules : Two numbers participating in the operation , If the two corresponding bits are “ different ”( Values are different ), Then the result of this bit is 1, Otherwise 0.

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

 example : 2 ^ 4  namely  00000010 ^ 00000100 =00000110 , therefore  2 ^ 4  The value of is 6 .

 summary : Two objects involved in the operation , If two corresponding bits are the same, it is 0, Dissimilarity is 1

 Several properties of XOR :
1、 Commutative law 
2、 Associative law  (a^b)^c == a^(b^c)
3、 For any number x, There are  x^x=0,x^0=x
4、 reflexivity : a^b^b=a^0=a

The purpose of exclusive or operation :

1) Flip refers to positioning

Like counting X=1010 1110 It's low 4 Bit to flip , Just find another number Y, Make Y It's low 4 Position as 1, The rest are 0, namely Y=0000 1111, And then X And Y Xor operation (X^Y=1010 0001) You can get .

2) And 0 Different or constant

for example :1010 1110 ^ 0000 0000 = 1010 1110

3) Exchange two numbers

int a = 10;
int b = 15;

if (a != b){
    a ^= b;
    b ^= a;
    a ^= b;
}
System.out.println(a);
System.out.println(b);

 result :
15
10

Negation operator (~)

Definition : A piece of data that participates in an operation , By binary " Take the opposite " operation .

Operational rules : 

~ 1 = 0
~ 0 = 1

 summary : Bitwise negate a binary number , the 0 change 1,1 change 0.

The purpose of exclusive or operation :

1) To make the lowest bit of a number zero

send a The lowest point is 0, It can be expressed as :a & ~1.~1 The value of is 1111 1111 1111 1110, Press again " And " operation , The lowest position must be 0. because " ~" The precedence of operators is higher than that of arithmetic operators 、 Relational operator 、 Logical operators and other operators are high .

Shift left operator (<<)

Definition : Move all binary bits of an operand to the left by several bits ( The left binary bits are discarded , The right to repair 0).

 set up  a = 1010 1110,a = a << 2  take a The binary bit of is shifted left 2 position 、 Right mend 0, Immediate a = 1011 1000.

Positive integer shifts left

 Before shifting : Decimal system 10   Binary system 1010

 Move two places to the left : Decimal system 40   Binary system 101000

Be careful : If the high position that is discarded when moving left does not include 1, Move one bit to the left , It's the number times 2. Shift left oldNum *2∧n

Negative integer shift left

 Before shifting : Decimal system -10   Binary system 11111111111111111111111111110110
 Move two places to the left : Decimal system -40   Binary system 11111111111111111111111111011000

Be careful :Java Negative numbers are stored in the form of complements ( Complement code = Inverse code +1) int Type account 4 Bytes 32 position

When int When type moves left , Left shift is greater than or equal to 32 Bit operation , I will ask for the surplus first (%) Then move left . That is to say, shift left 32 Bit is equivalent to no shift operation , Move left 40 Bit is equivalent to a left shift 8 position (40%32=8).

When long When type moves left ,long The representation of type in binary is 64 Bit , So the cardinality of the remainder operation becomes 64, That is to say, shift left 64 Bit is equivalent to no shift , Move left 72 Bit is equivalent to a left shift 8 position (72%64=8).

int intValue = 733183670;// Write a random number 

System.out.println("intValue:" + (intValue));// Print intValue
System.out.println("intValue Move left 1 position :" + (intValue << 1));// Move left 1 position   1466367340
System.out.println("intValue Move left 8 position :" + (intValue << 8));// Move left 8 position   -1283541504
// When int Type shift left is greater than or equal to 32 Bit operation , Will be the remainder before the shift operation 
System.out.println("intValue Move left 32 position :" + (intValue << 32));// The rest is 32%32=0, It's equivalent to moving left 0 position ( No displacement ) 733183670
System.out.println("intValue Move left 40 position :" + (intValue << 40));// The rest is 40%32=8, It's equivalent to moving left 8 position   -1283541504
System.out.println("intValue Move left 64 position :" + (intValue << 64));// The rest is 64%32=0, It's equivalent to moving left 0 position ( No displacement )  733183670

long longValue = 733183670L;

System.out.println("longValue:" + (longValue));// Print longValue
System.out.println("longValue Move left 1 position :" + (longValue << 1));// Move left 1 position   1466367340
System.out.println("longValue Move left 8 position :" + (longValue << 8));// Move left 8 position   187695019520
// When long Type shift left is greater than or equal to 64 Bit operation , Will be the remainder before the shift operation 
System.out.println("longValue Move left 64 position :" + (longValue << 64));// The rest is 64%64=0, It's equivalent to moving left 0 position ( No displacement )  733183670
System.out.println("longValue Move left 72 position :" + (longValue << 72));// The rest is 72%64=8, It's equivalent to moving left 8 position   187695019520
System.out.println("longValue Move left 128 position :" + (longValue << 128));// The rest is 128%64=0, It's equivalent to moving left 0 position ( No displacement )  733183670

Be careful : Other kinds of plastic surgery byte,short It will be converted to int type (32 position ) And then move it .double,float It's very special in binary , So we can't do the shift operation , Report errors , Compile but .

Signed shift right operator (>>)

Definition : Move all binary bits of a number to the right by several bits , Positive left to fill 0, Negative left to fill 1, On the right side of the discarded .

 for example :a = a >> 2  take a To the right 2 position , Left complement 0  perhaps   Left complement 1 It depends on whether the shifted number is positive or negative .

 Move the operands one bit to the right , It's the number divided by 2. Shift right oldNum / 2∧n

Positive integer shift right

 Before shifting : Decimal system 10   Binary system 1010

 Move two places to the right : Decimal system 2   Binary system 10

Shift negative integer right

 Before shifting : Decimal system -10   Binary system 11111111111111111111111111110110

 Move two places to the right : Decimal system -3   Binary system 11111111111111111111111111111110
int intValue = 733183670;// Write a random number 

System.out.println("intValue:" + (intValue));// Print intValue
System.out.println("intValue Move right 1 position :" + (intValue >> 1));// Move right 1 position   366591835
System.out.println("intValue Move right 8 position :" + (intValue >> 8));// Move right 8 position   2863998
// When int Type shift right is greater than or equal to 32 Bit operation , Will be the remainder before the shift operation 
System.out.println("intValue Move right 32 position :" + (intValue >> 32));// The rest is 32%32=0, It's equivalent to moving right 0 position ( No displacement )  733183670
System.out.println("intValue Move right 40 position :" + (intValue >> 40));// The rest is 40%32=8, It's equivalent to moving right 8 position   2863998
System.out.println("intValue Move right 64 position :" + (intValue >> 64));// The rest is 64%32=0, It's equivalent to moving right 0 position ( No displacement )  733183670

long longValue = 733183670L;

System.out.println("longValue:" + (longValue));// Print longValue
System.out.println("longValue Move right 1 position :" + (longValue >> 1));// Move right 1 position   366591835
System.out.println("longValue Move right 8 position :" + (longValue >> 8));// Move right 8 position   2863998
// When long Type shift right is greater than or equal to 64 Bit operation , Will be the remainder before the shift operation 
System.out.println("longValue Move right 64 position :" + (longValue >> 64));// The rest is 64%64=0, It's equivalent to moving right 0 position ( No displacement )  733183670
System.out.println("longValue Move right 72 position :" + (longValue >> 72));// The rest is 72%64=8, It's equivalent to moving right 8 position   2863998
System.out.println("longValue Move right 128 position :" + (longValue >> 128));// The rest is 128%64=0, It's equivalent to moving right 0 position ( No displacement )  733183670

Unsigned shift right operator :>>>

Unsigned shift right , Whether it's positive or negative , It's used in high positions 0 A filling ( Ignore sign bit ).

原网站

版权声明
本文为[HLee]所创,转载请带上原文链接,感谢
https://yzsam.com/2021/04/20210430190559115C.html