This week's book giveaways are in the Angular and TypeScript and Web Services forums.
We're giving away four copies each of Programming with Types and The Design of Web APIs and have the authors on-line!
See this thread and this one for details.
Win a copy of Programming with Types this week in the Angular and TypeScript forum
or The Design of Web APIs in the Web Services forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Liutauras Vilda
  • Bear Bibeault
  • Paul Clapham
  • Jeanne Boyarsky
Sheriffs:
  • Junilu Lacar
  • Knute Snortum
  • Henry Wong
Saloon Keepers:
  • Ron McLeod
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
Bartenders:
  • Frits Walraven
  • Joe Ess
  • salvin francis

Journal Article - The SCJP Tip Line - Bit Shifting

 
Sheriff
Posts: 7023
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The just-released June 2004 edition of The JavaRanch Journal includes an article by Corey McGlone, "The SCJP Tip Line - Bit Shifting".

Please use this thread to comment on and discuss the article.
 
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Originally posted by Dirk Schreckmann:
The just-released June 2004 edition of The JavaRanch Journal includes an article by Corey McGlone, "The SCJP Tip Line - Bit Shifting".

Please use this thread to comment on and discuss the article.



Hi Dirk...
I think, there is a mistake in the article:
You wrote:

b2 = (byte)(b2 >>> 2);
b2, which was 00001101, becomes 00000110 (which is 6 in decimal)

But, Its must return 00000011 (which is 3 in decimal) or I'm confused?

Thanks...
 
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
yes,it's true. I have compiled the source code. And the result should be 3.
 
Ranch Hand
Posts: 37
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Can anyone post any real-world examples of using bit-shifting? It's always been something I've found neat and interesting from a computer science standpoint but never had a good reason to use it.
 
Ranch Hand
Posts: 1392
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Michael,

>>Can anyone post any real-world examples of using bit-shifting?

An Internet address is a 32-bit number. The external representation is written as 4 unsigned bytes, ranging from 0 to 255, separated by periods.

java.sun.com/128.11.159.83
java.sun.com/209.249.116.141
java.sun.com/209.249.116.142
java.sun.com/209.249.116.143

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 ]
 
Marlene Miller
Ranch Hand
Posts: 1392
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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:
Java file APIs (DOC, XLS, PDF, and many more)
https://products.aspose.com/total/java
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!