[英]Is it safe to compare an (uint32_t) with an hard-coded value?
I need to do bitewise operations on 32bit integers (that indeed represent chars , but whatever). 我需要对32位整数(确实代表chars ,但无论如何)进行按位操作。
Is the following kind of code safe? 以下代码安全吗?
uint32_t input;
input = ...;
if(input & 0x03000000) {
output = 0x40000000;
output |= (input & 0xFC000000) >> 2;
I mean, in the "if" statement, I am doing a bitwise operation on, on the left side, a uint32_t, and on the right side... I don't know! 我的意思是,在“ if”语句中,我在左侧的uint32_t和右侧上执行按位运算...我不知道!
So do you know the type and size (by that I mean on how much bytes is it stored) of hard-coded "0x03000000" ? 那么,您是否知道硬编码的“ 0x03000000”的类型和大小(即存储多少字节)?
Is it possible that some systems consider 0x03000000 as an int and hence code it only on 2 bytes, which would be catastrophic? 是否有可能某些系统将0x03000000视为一个int并因此仅在2个字节上对其进行编码,这将是灾难性的?
Is the following kind of code safe?
以下代码安全吗?
Yes, it is. 是的。
So do you know the type and size (by that I mean on how much bytes is it stored) of hard-coded "0x03000000" ?
那么,您是否知道硬编码的“ 0x03000000”的类型和大小(即存储多少字节)?
0x03000000
is int
on a system with 32-bit
int
and long
on a system with 16-bit
int
. 0x03000000
是int
与在系统上32-bit
int
和long
带的系统上16-bit
int
。
(As uint32_t
is present here I assume two's complement and CHAR_BIT
of 8
. Also I don't know any system with 16-bit
int
and 64-bit
long
.) (因为这里存在
uint32_t
所以我假设二进制数为2且CHAR_BIT
为8
而且我也不知道任何具有16-bit
int
和64-bit
long
。)
Is it possible that some systems consider 0x03000000 as an int and hence code it only on 2 bytes, which would be catastrophic?
是否有可能某些系统将0x03000000视为一个int并因此仅在2个字节上对其进行编码,这将是灾难性的?
See above on a 16-bit
int
system, 0x03000000
is a long
and is 32-bit
. 参见上面的
16-bit
int
系统, 0x03000000
是long
,是32-bit
。 An hexadecimal constant in C is the first type in which it can be represented: int
, unsigned int
, long
, unsigned long
, long long
, unsigned long long
C中的十六进制常数是可以表示的第一种类型:
int
, unsigned int
, long
, unsigned long
, long long
, unsigned long long
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.