由买买提看人间百态

boards

本页内容为未名空间相应帖子的节选和存档,一周内的贴子最多显示50字,超过一周显示500字 访问原贴
Programming版 - question about shift
相关主题
binary number questionA aimple C++ question
a question about bitwise operation这个地址咋回事?
请教for_each请教一个简单字符串程序 (转载)
reverse words, not the Microsoft one!!!为什么foo1可以而foo2不行?
c++ 得最基本问题菜鸟求教,一个c++的困惑
来,出个题Please help me take a look at the program
一个指向指针的指针的引用?C++ formatted output question
问个char*的问题function call?
相关话题的讨论汇总
话题: unsigned话题: char话题: short话题: endl话题: cout
进入Programming版参与讨论
1 (共1页)
i***h
发帖数: 12655
1
下面的程序我期待 c=129
为什么高位会是ff?
#include
using namespace std;
int
main()
{
cout << (unsigned short)(2>>1 | 1<<7) << endl;
char c = 2>>1 | 1<<7;
cout << "c = " << (unsigned short)c << endl;
cout << "c = " << hex << (unsigned short)c << endl;
}
>>> output:
129
c = 65409
c = ff81
c*****t
发帖数: 1879
2
char is signed by default.

【在 i***h 的大作中提到】
: 下面的程序我期待 c=129
: 为什么高位会是ff?
: #include
: using namespace std;
: int
: main()
: {
: cout << (unsigned short)(2>>1 | 1<<7) << endl;
: char c = 2>>1 | 1<<7;
: cout << "c = " << (unsigned short)c << endl;

i***h
发帖数: 12655
3
Thanks!

【在 c*****t 的大作中提到】
: char is signed by default.
t****t
发帖数: 6806
4
by default, char is signed or unsigned is implementation-defined...i've seen
unsigned "char" as well.
that said, char, signed char, unsigned char are *3* different types. don't
make any assumptions.

【在 c*****t 的大作中提到】
: char is signed by default.
c*****t
发帖数: 1879
5
true (and compilers usually have options), but in most cases, char
is signed by default.

seen

【在 t****t 的大作中提到】
: by default, char is signed or unsigned is implementation-defined...i've seen
: unsigned "char" as well.
: that said, char, signed char, unsigned char are *3* different types. don't
: make any assumptions.

t****t
发帖数: 6806
6
hehe, i know char is signed in most cases...but it's not beneficial to have
that knowledge.

【在 c*****t 的大作中提到】
: true (and compilers usually have options), but in most cases, char
: is signed by default.
:
: seen

i***h
发帖数: 12655
7
one more question about char
how to read numbers into char?
I did:
char s;
int foo;
cin >> foo;
s = foo;
Can this code be shorter? Can I get rid of the temporary foo variable?
thanks
G*******n
发帖数: 2041
8
为什么前面补1,而不是补零?
我试了一下,好像MSB是1,前面就补1; MSB是零,前面就补零, 是不是这样

【在 c*****t 的大作中提到】
: char is signed by default.
t****t
发帖数: 6806
9
>> of negative number is implementation-defined...

【在 G*******n 的大作中提到】
: 为什么前面补1,而不是补零?
: 我试了一下,好像MSB是1,前面就补1; MSB是零,前面就补零, 是不是这样

i***h
发帖数: 12655
10
他问的好像不是这个
a negative number is represented in computer by setting its MSB to 1.
Its absolute value is ~N+1.
For example, 4-bit number 1000
~1000 = 0111
0111+1 = 1000
So 1000 represents -1000 = -8 (decimal)
To extend to 8-bit, 1000 -> 1111 1000
~(1111 1000) = 0000 0111
+1 => 0000 1000 = 8 (decimal)
That's why if negative number it always add 1s to higher bits.

【在 t****t 的大作中提到】
: >> of negative number is implementation-defined...
h*******e
发帖数: 225
11
that's signed shift. there are unsigned shifts too.
for example, in x86 asm, SHL/SHR are unsigned, while SAR/SAL are signed.

【在 G*******n 的大作中提到】
: 为什么前面补1,而不是补零?
: 我试了一下,好像MSB是1,前面就补1; MSB是零,前面就补零, 是不是这样

G*******n
发帖数: 2041
12
I see your point.
char c = 2>>1 | 1<<7; //c = 0x81 = -127
unsigned short b = c;
可以看成是分两步完成的: short temp = c //temp = -127 = 0xff81
unsigned short b = temp; //b = 0xff81

【在 i***h 的大作中提到】
: 他问的好像不是这个
: a negative number is represented in computer by setting its MSB to 1.
: Its absolute value is ~N+1.
: For example, 4-bit number 1000
: ~1000 = 0111
: 0111+1 = 1000
: So 1000 represents -1000 = -8 (decimal)
: To extend to 8-bit, 1000 -> 1111 1000
: ~(1111 1000) = 0000 0111
: +1 => 0000 1000 = 8 (decimal)

h*******e
发帖数: 225
13
不要这样看,因为根本就没有这个temp,b = c的时候实际发生的是带符号扩展(如果
编译器不优化的话)。

【在 G*******n 的大作中提到】
: I see your point.
: char c = 2>>1 | 1<<7; //c = 0x81 = -127
: unsigned short b = c;
: 可以看成是分两步完成的: short temp = c //temp = -127 = 0xff81
: unsigned short b = temp; //b = 0xff81

1 (共1页)
进入Programming版参与讨论
相关主题
function call?c++ 得最基本问题
问个指针array 的简单问题来,出个题
这个在c++ const不变,不能用相同地址的变量改,咋做的一个指向指针的指针的引用?
int 和 unsigned int 比大小会出问题 c++问个char*的问题
binary number questionA aimple C++ question
a question about bitwise operation这个地址咋回事?
请教for_each请教一个简单字符串程序 (转载)
reverse words, not the Microsoft one!!!为什么foo1可以而foo2不行?
相关话题的讨论汇总
话题: unsigned话题: char话题: short话题: endl话题: cout