Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Bit-wise operations question: There are several instances where we would like to

ID: 3564517 • Letter: B

Question

Bit-wise operations question:

There are several instances where we would like to retain only some of the bits of an integer x. For instance, the last 3 bits of 109 equal 101(base 2)=5 (base 10). Given that x is an integer, write an expression that would give an integer that corresponds to the LAST 4 BITS of X.

= x & 0x0000000F (?????)

Now, assuming that x is an integer, write an expression that would give an integer that corresponds to the next 4 bits of x, i.e. the bits that correspond to the positions with values 2^4, 2^5, 2^6, 2^7

=  x & 0x000000F0 (?????)

If x is any integer, what is the result of the following operations?

i) x ^ x

ii) x | x

iii) x & x

iv) x << 2 assuming x is unsigned and the result fits in 32 bits

v) x >>> 2 assuming that x is unsigned

vi) ~x + 1 assuming that x is signed

Explanation / Answer

The first two are correct
X = X & 0x0000000F
X = X & 0x000000F0

Now the remaining part
i) x ^ x gives 0

ii) x | x gives x

iii)x & x gives x

iv)x << 2 will multiply x with 4 i.e., 22

v)x >>2 will divide x with 4 i.e, 22

vi)~x + 1 will give the 2's compliment of x