b****a 发帖数: 4465 | 1 giving an int, how to check if the 1st bit is set? don't make any assumption
regarding the size of int.
my answer is to check the int is positive or negative. is it right? |
x*********s 发帖数: 2604 | |
d*****t 发帖数: 41 | 3 这是假设输入的是32位的数,题目说don't make any assumption regarding the
size
of int.
(input>>(8*sizeof(a)-1))&1 == 1
【在 x*********s 的大作中提到】 : (a>>31 && 1) == 1
|
s***f 发帖数: 226 | 4 1st bit? MSB or LSB.
MSB测正负好像可以,LSB只要测奇偶就行了。 |
K******g 发帖数: 1870 | 5 (a>>(sizeof(int)-1))&1 == 1
assumption
【在 b****a 的大作中提到】 : giving an int, how to check if the 1st bit is set? don't make any assumption : regarding the size of int. : my answer is to check the int is positive or negative. is it right?
|
b****a 发帖数: 4465 | 6 when shifting the bit cross over the bytes, will the byte order (big/little
indian) has any effect?
【在 K******g 的大作中提到】 : (a>>(sizeof(int)-1))&1 == 1 : : assumption
|
s*****s 发帖数: 157 | 7 of course Not.
Endian, 不是a3
little
【在 b****a 的大作中提到】 : when shifting the bit cross over the bytes, will the byte order (big/little : indian) has any effect?
|
b****a 发帖数: 4465 | 8 So, if there is a 4 byte (32 bit) int a, the result will be the same for a>
>8 on both big endian and little endian machines?
【在 s*****s 的大作中提到】 : of course Not. : Endian, 不是a3 : : little
|
s*****s 发帖数: 157 | 9 you got it.
a>
【在 b****a 的大作中提到】 : So, if there is a 4 byte (32 bit) int a, the result will be the same for a> : >8 on both big endian and little endian machines?
|