Bitwise operators (>> << ~ & ^ |) and examples of __ bitwise operators

Source: Internet
Author: User

1. Shift operators: Operators << and operator >> operands can be any integer or character type. An integral elevation is performed on two operands, and the type returned is the type of the left operand promoted.

The value of the i<<j is the result of moving the bits in I to the left J-bit. Each time you overflow one bit from the leftmost end of I, fill a 0-bit at the right end of I.

The value of the i>>j is the result of shifting the bits in I to the right by the J bit. If I is an unsigned or non-negative number, the left end is required to make a 0-bit complement. If I is a negative number, the result is defined by the implementation.

For example:

unsigned int i,j;

i = 13; The binary number of I is: 0000000000001101

j = i << 2; The value of J is 52 and the binary is: 0000000000110100

j = i >> 2; The value of J is 32:0000000000000011

2. Bitwise negation operator (~) bitwise AND operator (&) bitwise OR operator (^) bitwise OR Operator (|): Each of these four operators performs a Boolean operation on each digit of the operand.

~ Operator: reverse operation on operand, replace each 0 with 1, and replace each 0 with 1;

& operator: Two operands corresponding bits perform logic and operations.

^ and | Operators: Both perform logic or operations on two operands, the difference being that when the bit of two operands is 1 o'clock, ^ produces 0 and | produces 1.


For example:

int I, j,k;

i = 21; The binary number is: 0000000000010101

j = 56; The binary number is: 0000000000111000

K = ~i; K = 655,142 number is: 1111111111101010

K = i ^ j; K = 452 System: 0000000000101101

K = i & J; K = 162 System: 0000000000010000

K = I |    J; K = 612 System: 0000000000111101


Priority: from high to Low: ~ & ^ |

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.