Answer: True. You have to remember some powers of two. A gigabyte is , but, the horror, six (6) is not an integral power of two. However, it's larger than and less than . So 6GB will require at least bits.
Answer: False. Overflow in 2's complement only occurs when two numbers with the same sign are added together and produce a sum with a different sign. The carry out from the msb position may occur when overflow does, but is not an indicator of overflow in general.
((a < 0) ? (a + 15) : a) >> 16
is equivalent to , where is declared to be an int.
Answer: False. This is equivalent to dividing by 16, but only if we correct the shift-we want to do an arithmetic shift right by 4 (four), not 16.
( y + ((x - y) & -(x < y) )
returns
the minimum of x and y.
Answer: True. In order to see this, we will make a few intermediate variables.
int ybig = -(x < y); \* all ones if (x < y) is true , else 0 *\ int diff_xy = (x-y); \* sign = 1 if y < x, else sign is zero *\ int bias = diff_xy & ybig; \* bias = 0 if y < x, else (x-y)*\ return (y+bias)
Yeah, that was a lot of help. So, here is how this creature works. On a 32 bit machine.
Case | ybig | bias | result |
x < y |
0xFFFF FFFF | (x-y) | x |
x = y |
0 | 0 | y |
x>y |
0 | 0 | y |