由买买提看人间百态

boards

本页内容为未名空间相应帖子的节选和存档,一周内的贴子最多显示50字,超过一周显示500字 访问原贴
Programming版 - binary number question
相关主题
question about shiftambiguous operators in c++
0 < -1 ? A c++ questioninterview questions
真是奇了怪了,VC编译器问题?浮点数运算等于0的问题
Java的POD比C++干净很多C++的一个问题
print double auto precision?c++ interview: iterator 和 pointer区别?
int 和 unsigned int 比大小会出问题 c++c++ pointers are iterators, why?
问个g++的问题C++ Q110: Add without +
C++隐式类型转换的规则?请教一道数学题- 数列连乘积小于平均数的乘方吗?
相关话题的讨论汇总
话题: shift话题: 31话题: binary
进入Programming版参与讨论
1 (共1页)
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
5
thanks...
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?

1 (共1页)
进入Programming版参与讨论
相关主题
请教一道数学题- 数列连乘积小于平均数的乘方吗?print double auto precision?
Python problem on 64 bit Linuxint 和 unsigned int 比大小会出问题 c++
[合集] 讨厌的WARNING: 在 MANAGED C++ 中写东东问个g++的问题
template metaprogramming 的问题C++隐式类型转换的规则?
question about shiftambiguous operators in c++
0 < -1 ? A c++ questioninterview questions
真是奇了怪了,VC编译器问题?浮点数运算等于0的问题
Java的POD比C++干净很多C++的一个问题
相关话题的讨论汇总
话题: shift话题: 31话题: binary