How to interpret this bit shift

jcm

I have the following bit shift:

1011 & (~0 << 2)

How do I work out the answer for this? In particular I am confused about what ~0 << 2 means - I know that the << operator is a bit shift, and that ~ represents 'not'.

What I have read is that ~0 is a sequence of 1s - but how is that true, and how many 1s are there??

David.Jones

Usually, an int is a 32-bit/4-byte value. So ~0 = 1111 1111 1111 1111 1111 1111 1111 1111

In your case it really doesn't matter.

You want to solve 1011 & (~0 << 2)

Let's go through your example in steps.

First thing that happens is the parenthesis:

(~0 << 2) 

This is the bits 1111 shifted left by two bits. When a lift shift occurs the new added bits are 0s. Therefore (~0 << 2) equals:

(1111 << 2) = 1100

Finally you just need to do a bitwise and between 1011 and 1100 which ends up as

1000 = 8

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Java

What are bitwise shift (bit-shift) operators and how do they work?

From Dev

bit shift vs multiplication

From Dev

Which bit actually shift the << operator?

From Dev

Strange behaviour with right bit shift

From Dev

Bit shift operator precedence in C

From Dev

Shift bit-wise operation

From Dev

Wrong answer for 64 bit shift

From Dev

Error using bit shift operator

From Dev

How to create or interpret custom data type (4bit signed)

From Dev

Javascript bit shift to 32 bits

From Dev

How does this approximation of division using bit shift operations work?

From Dev

bit mask left shift

From Dev

How to shift 32 bit int by 32 (yet again)

From Dev

verilog bit shift with 1

From Dev

How to interpret 4 bytes as a 32-bit float using Python

From Dev

How to interpret hex number byte array from left shift binary sum?

From Dev

bit shift vs multiplication

From Dev

Javascript/PHP Interpret 64 bit integer as float

From Dev

How to interpret this bit shift

From Dev

How to implement bit-shift in freemarker?

From Dev

how to bit shift the variable and form the whole value

From Dev

How to bit-shift an immediate value

From Dev

How to interpret hex number byte array from left shift binary sum?

From Dev

If we Shift a bit to left and left most becomes 1 then how to calculate?

From Dev

How to declare a list of 32 bit binary numbers and shift in Python?

From Dev

How to explain the bit right shift two different results?

From Dev

How to interpret using Most Significant Bit of single byte - Pcapng time format "if_tsresol"

From Dev

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

From Dev

How make an int primary key based on bit shift

Related Related

HotTag

Archive