Bitwise operation Inverse (~) with (&) XOR (^) or (|) Move right (>>) shift Left (<<)

Source: Internet
Author: User

first know the characteristics of these two binary data: 1=0000 0000 0000 0000 0000 0000 0000 0001

-1=1000 0000 0000 0000 0000 0000 0000 0001

1. The highest position (first) is positive or negative (0 is positive, 1 is negative)

2. Lowest bit (bottom) indicates parity (0 is even, 1 is odd)

One, bitwise reverse (~)

Decimal 1 bitwise reversed =?

Analysis:

1. Decimal 1 into binary: 1 = 0000 0000 0000 0000 0000 0000 0000 0001

2. bitwise REVERSE : The original binary system to get a new binary, the original is 0 is changed to 0.

1 bitwise inversion gets 1111 1111 1111 1111 1111 1111 1111 1110

3. The highest digit of the binary (first) is positive or negative (1 is negative, 0 is positive), if it is negative, if it is negative, it needs to be represented by the complement of the computer.

complement : complement = sign bit (highest bit) after bitwise inversion plus 1.

complement 1000 0000 0000 0000 0000 0000 0000 0010

4. The result is: the complement is converted to decimal:-2;

~1==-2

Decimal-1 bitwise reversed =?

1.-1 of the binary is 1000 0000 0000 0000 0000 0000 0000 0001

2. Negative numbers are stored in the form of their complement .

1 stored in the computer is 1111 1111 1111 1111 1111 1111 1111 1111

3. Bitwise REVERSE: 0000 0000 0000 0000 0000 0000 0000 0000

4. The result is: decimal: 0

~-1==0

Ii. bitwise AND AND (&)

Bitwise AND and (&): Two digits of the same bit are 1, or 1; if one is not 1, then 0.

25&3=> 25 = 0000 0000 0000 0000 0000 0000 0001 1001

3= 0000 0000 0000 0000 0000 0000 0000 0011

-------------------------------------------------------------------------------------

0000 0000 0000 0000 0000 0000 0000 0001

25&3==1

Third, bitwise XOR or (^)

Bitwise XOR (^): The same bit differs by 1 and the same is 0

25^3=> 25 = 0000 0000 0000 0000 0000 0000 0001 1001

3= 0000 0000 0000 0000 0000 0000 0000 0011

-------------------------------------------------------------------------------------

0000 0000 0000 0000 0000 0000 0001 1010

25&3==26

Four, bitwise OR (|)

Bitwise OR (|): The same bit as long as one is 1 is 1.

25|3=> 25 = 0000 0000 0000 0000 0000 0000 0001 1001

3= 0000 0000 0000 0000 0000 0000 0000 0011

-------------------------------------------------------------------------------------

0000 0000 0000 0000 0000 0000 0001 1011

25|3==27

v. Right SHIFT (>>)

Shift the position of 1 to the right n, and leave it out.

15=0000 0000 0000 0000 0000 0000 0000 1111

15>>1=0000 0000 0000 0000 0000 0000 0000 0111

15>>1=7

: The right shift is 1 bits divided by 21, and rounded.

n>>4==n/(24)

64>>4==4

VI. Shift Left (<<)

Shift the position of 1 to the left N, and leave it out.

: 1 bits to the left is multiplied by 21, and rounded.

N<<4==n*24

2<<4==32

Bitwise operation Inverse (~) with (&) XOR (^) or (|) Move right (>>) shift Left (<<)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.