简体   繁体   中英

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).

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!

So do you know the type and size (by that I mean on how much bytes is it stored) of hard-coded "0x03000000" ?

Is it possible that some systems consider 0x03000000 as an int and hence code it only on 2 bytes, which would be catastrophic?

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 is int on a system with 32-bit int and long on a system with 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 .)

Is it possible that some systems consider 0x03000000 as an int and hence code it only on 2 bytes, which would be catastrophic?

See above on a 16-bit int system, 0x03000000 is a long and is 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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM