简体   繁体   中英

Extracting 3rd digit of a hexadecimal number

I want to extract the 3rd digit of a hex number. For example, extract 4 from 0x4598 .

To extract 0th digit: (0x4598 & 0x0f) // returns 8

To extract 1st digit: (0x4598 & 0xf0) >> 4 // returns 9

To extract 2nd digit: (0x4598 & 0xf00) >> 8 // returns 5

For the 3rd digit, I followed the pattern and tried (0x4598 & 0xf000) >> 16 , but it returns 0 . What is wrong?

The pattern here would be to add four, not to double.

Try this instead:

(0x4598 & 0xf000) >> 12;

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