简体   繁体   中英

How to use bitwise operators to isolate bits from unsigned ints in C?

this seems to be working well for me except this one time. I'm trying to get the first 10 bits of this number

unsigned char c= 17512807u<<22>>22;

I expect this to be 359 or 0101100111 but I'm getting 103 or 0001100111. Is there a reason that this is happening?

The result you are getting is correct due to the truncation to 8-bit unsigned char value (when assigning to c ).

If you need a value that is 10-bits wide you should use a different datatype, like uint16_t or an unsigned int .

Reference to C++ integer datatypes

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