C + + bit operator details (XOR and shift operators) _c language

Source: Internet
Author: User

What is a bitwise operation

Bitwise operators are operated in binary, which can only be used for integer-type operations. such as: Char,short,int,long

Bitwise operators are used to get high and low values

int a=0x1234;

int high,low;

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

Low = A & 0x00ff;

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

The left shift is a binary number, move a number of bits, the right space out of the position filled with 0来, high left-shift overflow should discard the high.

such as: Inta = 8, a = 00001000;

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

In the left-shift process, if this number overflows and discards the high level that does not contain 1, then the value of the move is the number of digits *2^ the value.

Right shift is a binary number, the right to move a number of bits, the left vacated position filled with 0来 (if it is unsigned type, if the signed type is based on the operating system requirements to complement.) Some operating systems are filled with symbol bits, and some operating systems are 0来 filled by default.

different or (^)

Two values that participate in binary operations are equal to 0, otherwise 1

1. With 0 different or, retain the original value

2. Exchange two values without a temporary variable

A=3,b=4

A= a^b;

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

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

The above C + + bit operator detailed explanation (XOR operator and shift operator) is a small series to share all the content, hope to give you a reference, but also hope that we support the cloud-dwelling community.

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.