Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Interpreting hex values?

Tags:

c

This might be very basic or even silly to experts here but I wanted to get my head around this. Most of the times I generally write hex values like this in C:

unsigned int a = 0xFFFF1232;

Let's say I am trying to extract the first and last 16-bits then I can simply do:

unsigned short high = a >> 16; // Gives me 0xFFFF
unsigned short low = a & 0x0000FFFF; // Gives me 0x00001232 which is then stored as 0x1232

In some of the code I am reading I have come across the following:

unsigned short high = a >> 16; 
unsigned short low = a & 0xFFFF; 

I have two questions

  • When you are ANDing a 32-bit value with a mask, why do people write 0xFFFF instead of 0x0000FFFF? Is it to keep it compact?
  • Is it always safe to write 0x0000FFFF as 0xFFFF? Is it interpreted differently in any context?
like image 365
Legend Avatar asked Dec 03 '10 22:12

Legend


People also ask

What do the numbers in hex mean?

The first nine numbers (0 to 9) are the same ones commonly used in the decimal system. The next six two-digit numbers (10 to 15) are represented by the letters A through F. This is how the hex system uses the numbers from 0 to 9 and the capital letters A to F to represent the equivalent decimal number.

Does hex mean 6 or 16?

The hexadecimal numeral system, often shortened to "hex", is a numeral system made up of 16 symbols (base 16). The standard numeral system is called decimal (base 10) and uses ten symbols: 0,1,2,3,4,5,6,7,8,9. Hexadecimal uses the decimal numbers and six extra symbols.

How do you read a hexadecimal address?

Memory addresses are displayed as two hex numbers. An example is C800:5. The part to the left of the colon (C800) is called the segment address, and the part to the right of the colon (5) is called the offset. The offset value can have as many as four hex digits.


1 Answers

They're completely synonymous. Leaving out the leading zeros makes it a little more readable.

like image 188
Russell Borogove Avatar answered Oct 13 '22 15:10

Russell Borogove