我正在读一本书,练习如下:
/* howManyBits - return the minimum number of bits required to represent x in
* two's complement
* Examples: howManyBits(12) = 5
* howManyBits(0) = 1
* howManyBits(-1) = 1
* howManyBits(0x80000000) = 32
* Legal ops: ! ~ & ^ | + << >>
* Max ops: 90
* Rating: 4
*/
int howManyBits(int x) {
return 0;
}
我什至不了解问题本身,为什么12需要5位,不是1100,即4位?而为什么-1只需要1位呢? 1 ... 1不是二进制补码中的-1,所以需要32位吗?