William Jones
William Jones

Reputation: 11

zero's inbetween int literal in C

What does the following mean in C programming language? I understand, that in my first line I have a hex-literal I don't really understand what my 2nd line is doing. Without actually running the code, how can I find out what this code is doing? I'm studying for an exam where I will have to do this on paper.

int aaa = 0x5c0000a3;
printf( "%08x %08x\n", (aaa >> 12), (aaa << 16) ); 

Upvotes: 0

Views: 61

Answers (2)

asio_guy
asio_guy

Reputation: 3767

these are the bit-shift operators or simply shift-operators

  • Right shift (>>)
  • Left shift (<<)

now your code

int aaa = 0x5c0000a3;

in binary aaa will look like this

1011100000000000000000010100011

in printf there are two expressions the first expression (aaa >> 12) shifts 12 bits towards right filling 12 bits from the left most side to zeros, resulting in a binary value like

1011100000000000000

which when converted to hex will results in 0x5C000, the corresponding format specifier for this is %08x which will pad zeros from left and will print 0005c000 to console

similar thing happens when left shift applied, where aaa is shifted towards left while padding 16 zero zero bits from right.

Upvotes: 0

Roderick Lenz
Roderick Lenz

Reputation: 159

In the second line: ">>" indicates a bit shift to the right, "<<" is a bit shift to the left. So it's going to print a string to the console with the value of aaa shifted 12 right then shifted 16 left.

To expand: convert the hex value to binary, shift every bit right 12, convert to an int with 8 places. Then shift all bits to the left 16 places and convert again.

Upvotes: 1

Related Questions