c*******9 发帖数: 6411 | 1 int a = -20;
c = (a>>31);
running the code get c = -1, and a and c's binary presentation are:
11111111111111111111111111101100 (a)
11111111111111111111111111111111 (c)
my question is a>>31 should be 00000000000000000000000000000001, how come
the c value is -1.
thanks. |
t****t 发帖数: 6806 | 2 usually (read: implementation-defined) >> on negative integer will keep the
sign bit unchanged.
to get 1, you need to do it on unsigned number.
【在 c*******9 的大作中提到】 : int a = -20; : c = (a>>31); : running the code get c = -1, and a and c's binary presentation are: : 11111111111111111111111111101100 (a) : 11111111111111111111111111111111 (c) : my question is a>>31 should be 00000000000000000000000000000001, how come : the c value is -1. : : thanks.
|
c*******9 发帖数: 6411 | 3 so here a is 11111111111111111111111111101100
a>>31 should be 00000000000000000000000000000001, since a is negative, so
add back the negative sign -> 10000000000000000000000000000001 which is -1
here...
Is that what you mean?
Thank you... |
t****t 发帖数: 6806 | 4 no, every "one bit shift" will keep the sign bit.
1
【在 c*******9 的大作中提到】 : so here a is 11111111111111111111111111101100 : a>>31 should be 00000000000000000000000000000001, since a is negative, so : add back the negative sign -> 10000000000000000000000000000001 which is -1 : here... : Is that what you mean? : Thank you...
|
c*******9 发帖数: 6411 | |
a****l 发帖数: 8211 | 6 你这里犯了两个错:
1)前面加1是正确的,否则你的计算就乱套了 (-20/2=10?? ).
2)前面加1不是一定保证的,也就是说如果你的cpu/compiler是某种特别的型号的话,有
可能最后出来的结果是前面是加0的.
【在 c*******9 的大作中提到】 : int a = -20; : c = (a>>31); : running the code get c = -1, and a and c's binary presentation are: : 11111111111111111111111111101100 (a) : 11111111111111111111111111111111 (c) : my question is a>>31 should be 00000000000000000000000000000001, how come : the c value is -1. : : thanks.
|
g*********s 发帖数: 1782 | 7 右移有两种。
【在 a****l 的大作中提到】 : 你这里犯了两个错: : 1)前面加1是正确的,否则你的计算就乱套了 (-20/2=10?? ). : 2)前面加1不是一定保证的,也就是说如果你的cpu/compiler是某种特别的型号的话,有 : 可能最后出来的结果是前面是加0的.
|
a****l 发帖数: 8211 | 8 are you talking about a certain cpu implementation , or the C standard?
【在 g*********s 的大作中提到】 : 右移有两种。
|
g*********s 发帖数: 1782 | 9 i guess cpu has two instructions. a common practice would be:
unsigned int x = -1; x >> 31; // compiler map to logic_shift
int x = -1; x >> 31; // compiler map to arithmetic_shift
for x86:
SAL Shift Arithmetically left (signed shift left)
SAR Shift Arithmetically right (signed shift right)
SHL Shift left (unsigned shift left)
SHR Shift right (unsigned shift right)
but i don't know why SAL is needed. possibly put sign on the right-most
bit?
c standard is undefined, i think.
【在 a****l 的大作中提到】 : are you talking about a certain cpu implementation , or the C standard?
|
a****l 发帖数: 8211 | 10 great.BTW, what's the result of "int x=-1;x>>1;"? -1 or 0?
【在 g*********s 的大作中提到】 : i guess cpu has two instructions. a common practice would be: : unsigned int x = -1; x >> 31; // compiler map to logic_shift : int x = -1; x >> 31; // compiler map to arithmetic_shift : for x86: : SAL Shift Arithmetically left (signed shift left) : SAR Shift Arithmetically right (signed shift right) : SHL Shift left (unsigned shift left) : SHR Shift right (unsigned shift right) : but i don't know why SAL is needed. possibly put sign on the right-most : bit?
|