Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

what does a[0] = addr & 0xff?

i'm currently learning from the book "the shellcoder's handbook", I have a strong understanding of c but recently I came across a piece of code that I can't grasp.

Here is the piece of code:

          char a[4];
          unsigned int addr = 0x0806d3b0;
          a[0] = addr & 0xff;
          a[1] = (addr & 0xff00) >> 8; 
          a[2] = (addr & 0xff0000) >> 16;
          a[3] = (addr) >> 24;

So the question is what does this, what is addr & 0xff (and the three lines below it) and what makes >> 8 to it (I know that it divides it 8 times by 2)? Ps: don't hesitate to tell me if you have ideas for the tags that I should use.

like image 604
Th3Meg4ph0re Avatar asked Nov 23 '15 10:11

Th3Meg4ph0re


People also ask

What is a 0 address?

Zero-address instruction is a format of machine instruction. It has one opcode and no address fields.

Is 0 a valid subnet?

Some people may even try to argue that it is an invalid IP address because there is a 0 in third octet. However, today, this IP address is perfectly legal when it comes to subnetting.

Can 0 be used in an IP address?

0 or . 255 are not usable… in most cases! The reason for this is because we tend to allocate full Class C type addresses to our networks, making it obviously very simple for us to administer. A standard Class C network consists of 256 addresses (0 to 255 inclusive), of which one is the network address (.

What is the use of 0.0 0.0 IP address?

In the Internet Protocol Version 4, the address 0.0.0.0 is a non-routable meta-address used to designate an invalid, unknown or non-applicable target. It may also be used for when the client is offline. This address is assigned specific meanings in a number of contexts, such as on clients or on servers.


1 Answers

The variable addr is 32 bits of data, while each element in the array a is 8 bits. What the code does is copy the 32 bits of addr into the array a, one byte at a time.

Lets take this line:

a[1] = (addr & 0xff00) >> 8; 

And then do it step by step.

  1. addr & 0xff00 This gets the bits 8 to 15 of the value in addr, the result after the operation is 0x0000d300.
  2. >> 8 This shifts the bits to the right, so 0x0000d300 becomes 0x000000d3.
  3. Assign the resulting value of the mask and shift to a[1].
like image 190
Some programmer dude Avatar answered Oct 13 '22 00:10

Some programmer dude