<< and >> in C++

What does this = operator mean in C?

The expression set >>= 1; means set = set >> 1; that is right shift bits of set by 1 (self assigned form of >> bitwise right shift operator check Bitwise Shift Operators).

Suppose if set is:

BIT NUMBER    31   n=27        m=17                 0
▼ ▼ ▼ ▼
set = 0000 1111 1111 1110 0000 0000 0000 0000

Then after set >> = 1; variable set becomes:

BIT NUMBER    31   n=26        m=16                 0
▼ ▼ ▼ ▼
set = 0000 0111 1111 1111 0000 0000 0000 0000

Notice the bits number shifted.

Note a interesting point: Because set is unsigned long so this >> operation should be logical shift( unsigned shift) a logical shift does not preserve a number's sign bit.

Additionally, because you are shifting all bits to right (towards lower significant number) so one right shift is = divide number by two.

check this code (just to demonstrate last point):

int main(){
unsigned long set = 268304384UL;
set >>= 1;
printf(" set :%lu \n", set);
set = 268304384UL;
set /= 2;
printf(" set :%lu \n", set);
return 1;
}

And output:

 set :134152192 
set :134152192

(note: its doesn't means >> and / are both same)

Similarly you have operator <<= for left shift, check other available Bitwise operators and Compound assignment operators, also check section: bit expressions and difference between: signed/arithmetic shift and unsigned shift.

What does '' mean in C?

<< is the left shift operator. It is shifting the number 1 to the left 0 bits, which is equivalent to the number 1.

It is commonly used to create flags, numbers that can be combined together with | (bit or) and various operations can be applied to them, such as testing whether a flag is set, setting a flag, removing a flag, etc.

The reason that they can be combined together without interfering with each other is that each one is a power of two, and that is the reason for using 1 << x, because that yields powers of two:

1 << 0 == 20 == 1 == binary 0001
1 << 1 == 21 == 2 == binary 0010
1 << 2 == 22 == 4 == binary 0100
1 << 3 == 23 == 8 == binary 1000
etc

You can read about bit flags here: http://www.codeproject.com/KB/tips/Binary_Guide.aspx

C++: what does (ab) mean?

1 << 1 means:

00000000 00000001 changes to 00000000 00000010

1 << 8 means:

00000000 00000001 changes to 00000001 00000000

It's a bit shift operation. For every 1 on the right, you can think of yourself as multiplying the value on the left by 2. So, 2 << 1 = 4 and 2 << 2 = 8. This is much more efficient than doing 1 * 2.

Also, you can do 4 >> 1 = 2 (and 5 >> 1 = 2 since you round down) as the inverse operation.

Bitwise shift left and right in the same statement

The syntax is fine (although hard to read) and it will be parsed as c2 = (i1 << 8) >> 24.

So it will left shift i1 8 positions, thereby shifting the leftmost 8 bits off the lefthand edge, and then right shift the result 24 positions, thereby shifting the rightmost 16 bits off the righthand edge. If that's what you wanted, then you're good. I'd use parentheses to make it more readable.

If you're just going to convert that to a char, it's not obvious why you feel the need to remove the high-order bits (although it is true that there may be architectures in which int and char are the same size.)

Also, as noted by John Bollinger, it is possible for the end result to be larger than can fit in a char, which is not well defined in the common case that char is a signed type. (That will be true even if unsigned int is 32 bits, because you technically cannot assign a value greater than 127 to an 8-bit signed character.)

What is (x & 1) and (x = 1)?

These are Bitwise Operators (reference).

x & 1 produces a value that is either 1 or 0, depending on the least significant bit of x: if the last bit is 1, the result of x & 1 is 1; otherwise, it is 0. This is a bitwise AND operation.

x >>= 1 means "set x to itself shifted by one bit to the right". The expression evaluates to the new value of x after the shift.

Note: The value of the most significant bit after the shift is zero for values of unsigned type. For values of signed type the most significant bit is copied from the sign bit of the value prior to shifting as part of sign extension, so the loop will never finish if x is a signed type, and the initial value is negative.

Left shift operator in C

Left shifts do not truncate the number to fit the length of the original one. To get 90, use:

(a<<4) & 0xff

0x59 is an int and probably on your platform it has sizeof(int)==4. Then it's a 0x00000059. Left shifting it by 4 gives 0x00000590.

Also, form a good habit of using unsigned int types when dealing with bitwise operators, unless you know what you are doing. They have different behaviours in situations like a right shift.



Related Topics



Leave a reply



Submit