Left Shift IP Address

I found the following in some old and poorly documented C code:

#define addr (((((147 << 8) | 87) << 8) | 117) << 8) | 107

What is it? I know this is an IP address, and an offset of 8 bits to the left makes sense. But can anyone explain this to me as a whole? What is happening there?

Thank!

+3
source share
2 answers

The code

(((((147 << 8) | 87) << 8) | 117) << 8) | 107

generates 4 bytes containing IP 147.87.117.107. The first step is the innermost bracket:

147<<8
147 = 1001 0011
1001 0011 << 8 = 1001 0011 0000 0000

The second byte 87 is inserted bitwise or runs on (147 <8). As you can see, the 8 bits on the right are all 0 (due to <8), so a bitwise or operation just inserts 8 bits out of 87:

1001 0011 0000 0000  (147<<8)
0000 0000 0101 0111  (87)
-------------------  bitwise-or
1001 0011 0101 0111  (147<<8)|87

The same thing is done with rest, so you have 4 bytes at the end, stored in a single 32-bit integer.

+10
source

IPv4 , , 32- . IP- (147.87.117.107) - - OR "" 4- .

(: 107.117.87.147 - , .)

() :

aabb ccdd

aa - 147 (0x93), bb - 87 (0x57), cc - 117 (0x75), dd - 107 (0x6b), 9357756b.

. IPv6, IPv6 128 32.

+3

All Articles