由买买提看人间百态

boards

本页内容为未名空间相应帖子的节选和存档,一周内的贴子最多显示50字,超过一周显示500字 访问原贴
Programming版 - int 和 unsigned int 比大小会出问题 c++
相关主题
string operator +问几句汇编指令(assembly language)
0 < -1 ? A c++ question两个古董的C优化代码,现在还有效果么?
question about shift[合集] 3-4G内存的机器,到底跑64位还是32位OS快?
binary number questionC++ software engineer 3 years expectation
一道面试题以下两个C 代码是不是完全等价?
真是奇了怪了,VC编译器问题?C编译器为何允许一个函数在某个路径不返回值?
unsigned 8bit integer转换成signed 16bit应该是什么结果?CIH源代码
一个integer promotion问题GCC 居然允许变量长度的向量
相关话题的讨论汇总
话题: int话题: unsigned话题: signed话题: eax话题: convert
进入Programming版参与讨论
1 (共1页)
N******K
发帖数: 10202
1
int i =-1;
unsigned int j =0
i>=j 输出true
i>=int(j) 才对
l*********s
发帖数: 5409
2
because int is converted to unsigned int before comparsion.
z*y
发帖数: 1311
3
and your solution is not correct either
unsigned int j = 2147483648;
int i = 1;
(i > (int)j)
return true
N******K
发帖数: 10202
4
这种优先级很脑残

【在 l*********s 的大作中提到】
: because int is converted to unsigned int before comparsion.
p***o
发帖数: 1252
5
没warning么?

【在 N******K 的大作中提到】
: 这种优先级很脑残
N******K
发帖数: 10202
6
这么说来 用 unsigned int 很成问题

【在 z*y 的大作中提到】
: and your solution is not correct either
: unsigned int j = 2147483648;
: int i = 1;
: (i > (int)j)
: return true

N******K
发帖数: 10202
7


【在 p***o 的大作中提到】
: 没warning么?
t****t
发帖数: 6806
8
有warning你还抱怨什么. 不管是把int转成unsigned还是反过来, 都会有问题的.

【在 N******K 的大作中提到】
: 有
N******K
发帖数: 10202
9
我把程序里面所有 unsigned int 都换成了int
互相倒腾都会出错

【在 t****t 的大作中提到】
: 有warning你还抱怨什么. 不管是把int转成unsigned还是反过来, 都会有问题的.
L*****e
发帖数: 8347
10
没听懂,为啥说互相倒腾会出错?还是说你觉得它们之间convert的rule和你认为应该
的不一样?
你觉得int的-1 convert to unsigned int后应该是什么?

【在 N******K 的大作中提到】
: 我把程序里面所有 unsigned int 都换成了int
: 互相倒腾都会出错

相关主题
真是奇了怪了,VC编译器问题?问几句汇编指令(assembly language)
unsigned 8bit integer转换成signed 16bit应该是什么结果?两个古董的C优化代码,现在还有效果么?
一个integer promotion问题[合集] 3-4G内存的机器,到底跑64位还是32位OS快?
进入Programming版参与讨论
N******K
发帖数: 10202
11
我说的是数学计算意义上的转换

【在 L*****e 的大作中提到】
: 没听懂,为啥说互相倒腾会出错?还是说你觉得它们之间convert的rule和你认为应该
: 的不一样?
: 你觉得int的-1 convert to unsigned int后应该是什么?

L*****e
发帖数: 8347
12
convert signed int a to unsigned int时,如果signed是个负数的话,convert后就
是2^32 + a。
convert unsigned int a to signed int时,如果unsigned int比max signed int大的
时候,cast的原则是implementation defined,一般是把最高位convert成正负号,剩
余的31位数convert成int。

【在 N******K 的大作中提到】
: 我说的是数学计算意义上的转换
L*****e
发帖数: 8347
13
没法直接做数学意义上的转换,不管cast是从singed到unsigned,还是unsigned到
signed,都意味着丢失掉一部分信息,或者说是这部分信息已经转换成别的信息,
在这里就是负号被转成了更大的数。
如果你要signed int和unsigned int混在一起做数学意义的运算的话,可以把负数
当做减去一个正数,然后把这个正数cast成unsigned和别的unsigned的数做运算。。。

【在 N******K 的大作中提到】
: 我说的是数学计算意义上的转换
k**********g
发帖数: 989
14

Firstly, signed and unsigned int have a "common interval".
From zero to INT_MAX (0 .. 2147483647)
When both operands are within this common interval, comparison result will
be correct regardless of each operand's type, and/or the coercion rule.
Therefore, one only needs to check that each operand's value is within this
common interval (prior to coercion). This is how the machine code is
typically generated in languages that have integer range checking.
It is very simple to test this. Just test bit 31 (the highest bit) of the
integer.
In x86 assembly, one can use either:
TEST EAX, 0x80000000
and then testing EAX for non-zero,
or
SHR EAX, 31
and then testing EAX for non-zero.

【在 N******K 的大作中提到】
: int i =-1;
: unsigned int j =0
: i>=j 输出true
: i>=int(j) 才对

t****t
发帖数: 6806
15
i think he knows that pretty well, just whining...

this

【在 k**********g 的大作中提到】
:
: Firstly, signed and unsigned int have a "common interval".
: From zero to INT_MAX (0 .. 2147483647)
: When both operands are within this common interval, comparison result will
: be correct regardless of each operand's type, and/or the coercion rule.
: Therefore, one only needs to check that each operand's value is within this
: common interval (prior to coercion). This is how the machine code is
: typically generated in languages that have integer range checking.
: It is very simple to test this. Just test bit 31 (the highest bit) of the
: integer.

k**********g
发帖数: 989
16

I know ... just for the sake of other MITBBS hangouts
Problem is, compiler don't always give warnings all the time.
No matter you specify Wall Werror Wpedantic Wextra Wtf

【在 t****t 的大作中提到】
: i think he knows that pretty well, just whining...
:
: this

l******t
发帖数: 55733
17
抛异常

【在 L*****e 的大作中提到】
: 没听懂,为啥说互相倒腾会出错?还是说你觉得它们之间convert的rule和你认为应该
: 的不一样?
: 你觉得int的-1 convert to unsigned int后应该是什么?

O*******d
发帖数: 20343
18
很多compiler给警告。

【在 p***o 的大作中提到】
: 没warning么?
U***5
发帖数: 2796
19
right, LZ 少见多怪。

【在 t****t 的大作中提到】
: 有warning你还抱怨什么. 不管是把int转成unsigned还是反过来, 都会有问题的.
g*********e
发帖数: 14401
20
有些不会warning
相关主题
C++ software engineer 3 years expectationCIH源代码
以下两个C 代码是不是完全等价?GCC 居然允许变量长度的向量
C编译器为何允许一个函数在某个路径不返回值?is it possible to design a zero miss rate cache?
进入Programming版参与讨论
g*****y
发帖数: 7271
21
lz可能是希望彻底取缔unsigned吧?话说问题应该也不是很大吧,
如果大家都用64位系统的话。

【在 t****t 的大作中提到】
: i think he knows that pretty well, just whining...
:
: this

c****p
发帖数: 6474
22
位运算为主的情况下unsigned还是有用的吧?

【在 g*****y 的大作中提到】
: lz可能是希望彻底取缔unsigned吧?话说问题应该也不是很大吧,
: 如果大家都用64位系统的话。

T***1
发帖数: 445
23
如果较真对话,应该取缔的是signed
凭什么最高位的1要做符号位不做数字呢?
凭什么FFFFFFFFFFFFFFFF 不是 18446744073709551615 而非要人为定义成 -1 呢?

【在 g*****y 的大作中提到】
: lz可能是希望彻底取缔unsigned吧?话说问题应该也不是很大吧,
: 如果大家都用64位系统的话。

g****t
发帖数: 31659
24
Unsigned 和 signed的比较应该尽量去掉。
Code应该在尽可能少的假设下成立,才是好code。
Unsigned signed比较,需要假设固定的转换规则,才能成立。这个属于多余的假设。
留着这种code,今后你的CPU, C complier换了怎么办?

【在 N******K 的大作中提到】
: int i =-1;
: unsigned int j =0
: i>=j 输出true
: i>=int(j) 才对

N******K
发帖数: 10202
25
int i =-1;
unsigned int j =0
i>=j 输出true
i>=int(j) 才对
l*********s
发帖数: 5409
26
because int is converted to unsigned int before comparsion.
z*y
发帖数: 1311
27
and your solution is not correct either
unsigned int j = 2147483648;
int i = 1;
(i > (int)j)
return true
N******K
发帖数: 10202
28
这种优先级很脑残

【在 l*********s 的大作中提到】
: because int is converted to unsigned int before comparsion.
p***o
发帖数: 1252
29
没warning么?

【在 N******K 的大作中提到】
: 这种优先级很脑残
N******K
发帖数: 10202
30
这么说来 用 unsigned int 很成问题

【在 z*y 的大作中提到】
: and your solution is not correct either
: unsigned int j = 2147483648;
: int i = 1;
: (i > (int)j)
: return true

相关主题
C++疑问0 < -1 ? A c++ question
Do the two statements cost the same amount of time?question about shift
string operator +binary number question
进入Programming版参与讨论
N******K
发帖数: 10202
31


【在 p***o 的大作中提到】
: 没warning么?
t****t
发帖数: 6806
32
有warning你还抱怨什么. 不管是把int转成unsigned还是反过来, 都会有问题的.

【在 N******K 的大作中提到】
: 有
N******K
发帖数: 10202
33
我把程序里面所有 unsigned int 都换成了int
互相倒腾都会出错

【在 t****t 的大作中提到】
: 有warning你还抱怨什么. 不管是把int转成unsigned还是反过来, 都会有问题的.
L*****e
发帖数: 8347
34
没听懂,为啥说互相倒腾会出错?还是说你觉得它们之间convert的rule和你认为应该
的不一样?
你觉得int的-1 convert to unsigned int后应该是什么?

【在 N******K 的大作中提到】
: 我把程序里面所有 unsigned int 都换成了int
: 互相倒腾都会出错

N******K
发帖数: 10202
35
我说的是数学计算意义上的转换

【在 L*****e 的大作中提到】
: 没听懂,为啥说互相倒腾会出错?还是说你觉得它们之间convert的rule和你认为应该
: 的不一样?
: 你觉得int的-1 convert to unsigned int后应该是什么?

L*****e
发帖数: 8347
36
convert signed int a to unsigned int时,如果signed是个负数的话,convert后就
是2^32 + a。
convert unsigned int a to signed int时,如果unsigned int比max signed int大的
时候,cast的原则是implementation defined,一般是把最高位convert成正负号,剩
余的31位数convert成int。

【在 N******K 的大作中提到】
: 我说的是数学计算意义上的转换
L*****e
发帖数: 8347
37
没法直接做数学意义上的转换,不管cast是从singed到unsigned,还是unsigned到
signed,都意味着丢失掉一部分信息,或者说是这部分信息已经转换成别的信息,
在这里就是负号被转成了更大的数。
如果你要signed int和unsigned int混在一起做数学意义的运算的话,可以把负数
当做减去一个正数,然后把这个正数cast成unsigned和别的unsigned的数做运算。。。

【在 N******K 的大作中提到】
: 我说的是数学计算意义上的转换
k**********g
发帖数: 989
38

Firstly, signed and unsigned int have a "common interval".
From zero to INT_MAX (0 .. 2147483647)
When both operands are within this common interval, comparison result will
be correct regardless of each operand's type, and/or the coercion rule.
Therefore, one only needs to check that each operand's value is within this
common interval (prior to coercion). This is how the machine code is
typically generated in languages that have integer range checking.
It is very simple to test this. Just test bit 31 (the highest bit) of the
integer.
In x86 assembly, one can use either:
TEST EAX, 0x80000000
and then testing EAX for non-zero,
or
SHR EAX, 31
and then testing EAX for non-zero.

【在 N******K 的大作中提到】
: int i =-1;
: unsigned int j =0
: i>=j 输出true
: i>=int(j) 才对

t****t
发帖数: 6806
39
i think he knows that pretty well, just whining...

this

【在 k**********g 的大作中提到】
:
: Firstly, signed and unsigned int have a "common interval".
: From zero to INT_MAX (0 .. 2147483647)
: When both operands are within this common interval, comparison result will
: be correct regardless of each operand's type, and/or the coercion rule.
: Therefore, one only needs to check that each operand's value is within this
: common interval (prior to coercion). This is how the machine code is
: typically generated in languages that have integer range checking.
: It is very simple to test this. Just test bit 31 (the highest bit) of the
: integer.

k**********g
发帖数: 989
40

I know ... just for the sake of other MITBBS hangouts
Problem is, compiler don't always give warnings all the time.
No matter you specify Wall Werror Wpedantic Wextra Wtf

【在 t****t 的大作中提到】
: i think he knows that pretty well, just whining...
:
: this

相关主题
binary number questionunsigned 8bit integer转换成signed 16bit应该是什么结果?
一道面试题一个integer promotion问题
真是奇了怪了,VC编译器问题?问几句汇编指令(assembly language)
进入Programming版参与讨论
l******t
发帖数: 55733
41
抛异常

【在 L*****e 的大作中提到】
: 没听懂,为啥说互相倒腾会出错?还是说你觉得它们之间convert的rule和你认为应该
: 的不一样?
: 你觉得int的-1 convert to unsigned int后应该是什么?

O*******d
发帖数: 20343
42
很多compiler给警告。

【在 p***o 的大作中提到】
: 没warning么?
U***5
发帖数: 2796
43
right, LZ 少见多怪。

【在 t****t 的大作中提到】
: 有warning你还抱怨什么. 不管是把int转成unsigned还是反过来, 都会有问题的.
g*********e
发帖数: 14401
44
有些不会warning
g*****y
发帖数: 7271
45
lz可能是希望彻底取缔unsigned吧?话说问题应该也不是很大吧,
如果大家都用64位系统的话。

【在 t****t 的大作中提到】
: i think he knows that pretty well, just whining...
:
: this

c****p
发帖数: 6474
46
位运算为主的情况下unsigned还是有用的吧?

【在 g*****y 的大作中提到】
: lz可能是希望彻底取缔unsigned吧?话说问题应该也不是很大吧,
: 如果大家都用64位系统的话。

T***1
发帖数: 445
47
如果较真对话,应该取缔的是signed
凭什么最高位的1要做符号位不做数字呢?
凭什么FFFFFFFFFFFFFFFF 不是 18446744073709551615 而非要人为定义成 -1 呢?

【在 g*****y 的大作中提到】
: lz可能是希望彻底取缔unsigned吧?话说问题应该也不是很大吧,
: 如果大家都用64位系统的话。

g****t
发帖数: 31659
48
Unsigned 和 signed的比较应该尽量去掉。
Code应该在尽可能少的假设下成立,才是好code。
Unsigned signed比较,需要假设固定的转换规则,才能成立。这个属于多余的假设。
留着这种code,今后你的CPU, C complier换了怎么办?

【在 N******K 的大作中提到】
: int i =-1;
: unsigned int j =0
: i>=j 输出true
: i>=int(j) 才对

g*c
发帖数: 4510
49
奇怪为啥这种情况是吧signed convert成unsigned,而不是把unsigned convert成
signed。

【在 N******K 的大作中提到】
: int i =-1;
: unsigned int j =0
: i>=j 输出true
: i>=int(j) 才对

1 (共1页)
进入Programming版参与讨论
相关主题
GCC 居然允许变量长度的向量一道面试题
is it possible to design a zero miss rate cache?真是奇了怪了,VC编译器问题?
C++疑问unsigned 8bit integer转换成signed 16bit应该是什么结果?
Do the two statements cost the same amount of time?一个integer promotion问题
string operator +问几句汇编指令(assembly language)
0 < -1 ? A c++ question两个古董的C优化代码,现在还有效果么?
question about shift[合集] 3-4G内存的机器,到底跑64位还是32位OS快?
binary number questionC++ software engineer 3 years expectation
相关话题的讨论汇总
话题: int话题: unsigned话题: signed话题: eax话题: convert