In the source code of java.net.InetAddress, the Internet address is stored in a field of type int. When we call the toString() method, the 32-bit value is converted to 4 byte values, which are then converted to a String.
A good source of examples where you will find bit shifting is sending data over the network. Data is written as a stream of bytes. 32-bit int values and 16-bit Unicode characters are broken up into bytes by shifting 8 bits at a time.
In paeleolithic times, programmers shifted 1 instead of multiplying by 2 because shifting takes few clock cycles than multiplying. In C code, you would see a lot of x << 1 all over a program. [ June 19, 2004: Message edited by: Marlene Miller ]
posted 15 years ago
Another example of bit shifting...
I guess there are 5 different class of Internet address. The 32-bit address is partitioned into fields.
Class A: 0, netid 7-bits, hosted 24-bits Class B: 10, netid 14-bits, hostid 16-bit Class C: 110, netid 21-bits, hosted 8-bits Class D: 1110, multicast group ID 28-bits Class E: 11110, reserved for future use 27-bits
To extract the values of the fields, you will probably do some shifting.
I guess there are other ways to look at the 32-bit Internet address. Here is some code from java.net.InetAddress.
If you want some more ideas, search the source code of the Java APIs, which comes with the Java SDK. My search found about 900 lines of code.
Do not threaten THIS beaver! Not even with this tiny ad: