Shift bit-wise operation

stillAFanOfTheSimpsons

I'm learning c programming and now struggling with the bit-wise operation. Say if I want to shift short n >> by 4 and << by 10, and use the short result as the condition of a loop. i.e if (result==1) {//do something} do I need to declare another two variables, i.e. short right= n>>4; short left=... or is there a more proper way of writing this? Thanks!

Remy Lebeau

You do not need additional variables unless you intend to use the result of the shifts more than once.

Shifting right by 4 and then left by 10 is not the same as just shifting left by 6, so you have to do separate shifts if you want the side effect of shifting past the end of the right side to zero out some of the right-hand bits, eg:

short n = ...;
if (((n >> 4) << 10) == 1) {
    //do something
}

Let's say you start with 0xFFFF (1111111111111111). Shifting right by 4 produces 0x0FFF (0000111111111111), then shifting that left by 10 produces 0xFC00 (1111110000000000).

On the other hand, if you just shift left by 6:

short n = ...;
if ((n << 6) == 1) {
    //do something
}

0xFFFF becomes 0xFFC0 (1111111111000000) instead. Clearly not the same value.

So you have to be careful with your shifts.

BTW, shifting by these particular values will never produce a result of 1.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Bit-wise shift for Matrix iteration?

From Dev

Tensorflow bit-wise NOT operation for floats

From Dev

What does this bit wise operation doing?

From Dev

Bit update operation using bit wise coding style

From Dev

Java bit wise operator (zero fill shift) not filling with zeros

From Dev

How to choose the correct left shift in bit wise operations?

From Dev

h2 database bit wise operation(&) invalid syntax MySql

From Dev

Bit wise operation on float value on SQL Server 2016

From Dev

8 bit shift operation in AVX2 with shifting in zeros

From Dev

8 bit shift operation in AVX2 with shifting in zeros

From Dev

Why does bit-wise shift left return different results in Python and Java?

From Dev

Filtering of structured array via bit-wise operation (bitmask) on column data

From Dev

Column wise operation in Spark

From Dev

Why bool shows the correct bit order while outputting the shift operation result directly doesn't?

From Dev

Bit wise 'AND' an array of registers in Verilog

From Java

bit-wise negation in systemVerilog

From Dev

Byte swapping in bit wise operations

From Dev

bit shift vs multiplication

From Dev

How to interpret this bit shift

From Dev

bit mask left shift

From Dev

verilog bit shift with 1

From Dev

bit shift vs multiplication

From Dev

How to interpret this bit shift

From Dev

Bit-wise operators and bit manipulation

From Dev

Left shift operation

From Dev

Shift Operation in C++

From Dev

Bitwise shift operation choice

From Dev

Wrong answer for 64 bit shift

From Dev

Strange behaviour with right bit shift