C + + bit operations overview

Source: Internet
Author: User
Tags bitwise integer

To define a binary variable:

Typically defined as octal or hexadecimal, octal numbers begin with 0, and hexadecimal numbers begin with 0x

For example, int a = 0x80, where 80 can only represent 8 bits, it represents the lower 8 bits of int, 24 bits in front of 0, so a = 128, or a =–0x80, at this time a = -128;8 system

Note: If the 0x ... Can be expressed within the shape of the shape, the default is int, otherwise the unsigned int can be represented, followed by a long long, and then unsigned long long (can be Cout<<typeid (0xF0). Name () ; View variable types)

About shift Operations:

<< left shift operation: Start with 0 fill vacancies from right

>> Right shift operation: For unsigned numbers, start with 0 on the left; for signed digits, or for a complement sign bit, or for a complement of 0, determined by the compiler (GCC compiler is a complementary symbol bit)

Note: The number of shifts is negative or when the shift is out (up to only the type bits size-1), the operator's behavior is undefined, you can refer to the C + + shift operator, the bitwise operation is only for integer type (int long, etc.) or char type data

Common BITS operations (if not emphasized, expr can be an unsigned or signed integer): Please refer to the c_c++ problem--------the bitwise operation of the tricky question and its example (2)

1. Set the nth (n starting from 0) bit of expr to 1:expr |= (1<<n);

2. Set the nth (n starting from 0) bit of expr to 0:expr &= (~ (1<<n));

3. Determine whether the nth (n starting from 0) bit of expr is 1:bool B = expr & (1<<n);

4. Flip the nth (n starting from 0) bit of expr: expr ^= (1<<n);

5. Flip the rightmost 1 to 0:expr &= (expr-1) (which can be used to determine the number of 1 in binary, flip a 1 at a time, and know that the number changes to 0)

6. Continuous propagation of the rightmost 1 digits to the right: expr |= (expr-1) (This action makes 00101000 00101111)

7. Check whether the unsigned number expr is the integer power of 2: if ((expr& (expr-1)) ==0) return true; Which means that expr has only one 1 in its binary.

8. Flip the 1-bit string on the right to a 0-bit string, others remain unchanged: expr = (expr| ( expr-1)) (+1) &expr

9. Check if the unsigned integer expr equals the difference of two integer powers of 2 ((expr|) (  expr-1)) (+1) &expr = = 0) return true; (Just note: All 1 of the unsigned binary binaries are together)

10. For integer expr, the smallest, larger than expr integer m, so that the binary representation of M and expr have the same number of 1, as follows, the specific reference to the force! Efficient! Understand! Bitwise operation of a combination

int Nextn (int N)
{
    int x = n& (-n);     
    int t = n+x;
    return T | ((n^t)/x) >>2;
}

It should be noted that if there is no larger than expr and 1 of the same number in binary, the function returns-1

More Wonderful content: http://www.bianceng.cnhttp://www.bianceng.cn/Programming/cplus/

11. Circular shift, to shape the example, loop left and right shift function as follows

int rotateleft (int a, unsigned int n)/loop left shift n bit
{
    n%=;
    if (n = = 0) return A;
    Return (a << n) | ((A & 0xFFFFFFFF) >> (32-n));
}
    
int rotateright (int a, unsigned int n)/loop right shift n bit
{
    n%=;
    if (n = = 0) return A;
    Return ((A & 0xFFFFFFFF) >> N) | (A << (32-n));
}

For example:

A = 01111011, the correct result of the loop moving left 2 digits is: b=11101101

b = a >> (8-2); It is used to get the correct position//b=00000001 of the normal left-shift loss and the cyclic displacement.

A = a << 2; A = 11101100

A = a | b A = 11101101

Note 1: According to the above example, what we need is right-shift action right 0, but if the input is a negative number, C + + does not specify how the right shift operation is to complement, and most compilers are complementary sign bit, so at this time need to convert this negative number to unsigned integer, this is a & The function of the 0xFFFFFFFF (the type of 0xFFFFFFFF is unsigned int)

NOTE 2: When n is more than 32 o'clock, you need n = n%32 which is equivalent to looping around several laps, and if n = 0, then 32-n = 32, and the integer shift operation is undefined for the number of digits greater than 31 (see the section on the above shift operation Red Word annotation)

Author: cnblogs Tenos

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.