我可以使用unsigned char作为int吗? [英] Can I use unsigned char as int?
问题描述
例如,我想使用unsigned char作为8位整数。
我可以使用
unsigned char a = 0;
a ++
非常感谢!
For instance, I''d like to use unsigned char as an 8-bit integer.
Can I use like
unsigned char a=0;
a++
Thanks a lot!
推荐答案
po *********** @ gmail.com 于01/08/05写道:
po***********@gmail.com wrote on 01/08/05 :
例如,我想使用unsigned char作为8位整数。
我可以使用
unsigned char a = 0;
a ++
For instance, I''d like to use unsigned char as an 8-bit integer.
Can I use like
unsigned char a=0;
a++
从技术上讲,是的。保证的最小范围是0-255。但是我不希望b $ b推荐这种做法。 char不是''自然大小''而且可能会生成一些额外的
汇编代码以处理缩小的尺寸...(额外
masquing,签名时签名扩展等等。
我建议使用int或unsigned int来获得更高效的代码。
但是,小整数(如char)可能对数组减少内存打印,如果额外的代码在
性能方面不是问题。我们经常要在执行
效率和内存大小之间做出选择......
另一点。将参数定义为char是无关紧要的。它将
总是转换为int并将在自动
内存中使用int打印。
-
Emmanuel
C-FAQ: http://www.eskimo.com/~scs/C-faq/faq.html
C库: http://www.dinkumware.com/refxc.html
显然你的代码不符合原始规格。
你用湿面条判处30睫毛。
- alc ++中的Jerry Coffin
Technically, yes. The guaranteed minimum range is 0-255. But I don''t
recommand this practice. char are not ''natural sized'' and some extra
assembly code may be generated to deal with the reduced size... (extra
masquing, sign extension when signed etc.)
I recommend the use of int or unsigned int for a more efficient code.
However, small integers (like char) can be useful for arrays to reduce
the memory print, if the extra code is not a problem in terms of
performance. We often have to make a choice between execution
efficiency and memory size...
Another point. Defining a parameter as char is pointeless. It will
always be converted to an int and will use an int print in automatic
memory.
--
Emmanuel
The C-FAQ: http://www.eskimo.com/~scs/C-faq/faq.html
The C-library: http://www.dinkumware.com/refxc.html
"Clearly your code does not meet the original spec."
"You are sentenced to 30 lashes with a wet noodle."
-- Jerry Coffin in a.l.c.c++
非常感谢!我和其他人合作。
其他人定义了
#define Byte unsigned char
所以我必须使用它。
以下是更好的方法吗?
Byte a;
unsigned short tmp;
if(tmp> 256)
tmp = 0;
tmp ++
a = tmp;
Thanks a lot! I am cooperating with others.
other people have defined
#define Byte unsigned char
so I have to use it.
So is the following a better way?
Byte a;
unsigned short tmp;
if(tmp>256)
tmp = 0;
tmp ++
a = tmp;
Emmanuel Delahaye写道:
[...]
Emmanuel Delahaye wrote:
[...]
另一点。将参数定义为char是无关紧要的。它将始终转换为int并将在自动
内存中使用int打印。
Another point. Defining a parameter as char is pointeless. It will
always be converted to an int and will use an int print in automatic
memory.
这是否完全正确?
鉴于以下内容:
void foo(char c)
{
..用''c'做的东西......
}
void myfunc(无效)
{
foo(''a'');
}
假设32位整数和ASCII字符集,编译器必须
生成将0x00000061传递给foo的代码?
我见过编译器会将0x61加载到
寄存器的低字节并将该寄存器推入堆栈,剩下的只剩下
寄存器。是的,它确实推了一个32位的值,但最高的b / b 24位处于未确定的状态。
如果这些编译器坏了,就此而言标准是什么?
严格来说,C编译器生成
代码是合法的,它会在堆栈上推送一个16位值在一个32位
整数的系统上?
-
+ -------------- ----------- + -------------------- + ----------------- ------------ +
| Kenneth J. Brody | www.hvcomputer.com | |
| kenbrody / at\spamcop.net | www.fptech.com | #include< std_disclaimer.h> |
+ ------------------------- + -------------- ------ + ----------------------------- +
不要给我发电子邮件:< mailto:Th ************* @ gmail.com>
Is this strictly true?
Given the following:
void foo(char c)
{
... do stuff with ''c'' ...
}
void myfunc(void)
{
foo(''a'');
}
Assuming 32-bit ints and the ASCII character set, must the compiler
generate code that passes 0x00000061 to foo?
I have seen compilers which will load 0x61 into the low byte of a
register and push that register onto the stack, leaving the rest of
the register alone. Yes, it did push a 32-bit value, but the top
24 bits were in an undetermined state.
Were these compilers broken, as far as the standard goes?
And, strictly speaking, would it be legal for a C compiler to generate
code which pushed a 16-bit value on the stack on a system with 32-bit
integers?
--
+-------------------------+--------------------+-----------------------------+
| Kenneth J. Brody | www.hvcomputer.com | |
| kenbrody/at\spamcop.net | www.fptech.com | #include <std_disclaimer.h> |
+-------------------------+--------------------+-----------------------------+
Don''t e-mail me at: <mailto:Th*************@gmail.com>
这篇关于我可以使用unsigned char作为int吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!