wood burning stoves 2.0*
The moose likes Beginning Java and the fly likes applications of bitwise operators Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "applications of bitwise operators" Watch "applications of bitwise operators" New topic
Author

applications of bitwise operators

T Chakradhar Reddy
Greenhorn

Joined: Aug 31, 2007
Posts: 3
What are the applications of bitwise operators. Not many books go beyond explaining them. Where exactly are they used? and what are the advantages of using these operators(bitwise)?
Tony Docherty
Bartender

Joined: Aug 07, 2007
Posts: 2290
    
  49
An example of use can be seen in the 3 parameter Font constructor. The second parameter (the style parameter) is one of the defined constants PLAIN, BOLD, or ITALIC or a bitwise union of BOLD and ITALIC.
Henry Wong
author
Sheriff

Joined: Sep 28, 2004
Posts: 18848
    
  40

Hardware registers generally return values at certain bit locations. And of course, set value via bit locations. Graphics are done by turning on / off bits at particular bytes. Networking protocols uses particular bits in a packet. Disk drive protocols (ie. the SCSI CDB). Etc.

IMHO, bit operators are probably not needed for high level application developers. The control of hardware, graphics, networking, etc., are all provided via libraries. Now of course, maybe one day, you'll write those libraries.

Henry


Books: Java Threads, 3rd Edition, Jini in a Nutshell, and Java Gems (contributor)
Stan James
(instanceof Sidekick)
Ranch Hand

Joined: Jan 29, 2003
Posts: 8791
Back in mainframe assembler and Turbo Pascal I thought it was a cool optimization to store a bunch of booleans in a bitmap, one bit each. So instead of saying "if a == true" I'd say "if (bitmap & A) == A" ... more or less. Java's booleans are of an unspecified size, taking up some bytes of memory each, but I would use them over bitmaps until it was proven that a zillion booleans were eating up too much memory in a particular application.

Then again, I have used my legacy knowledge of these things recently. I had several different attributes that could contribute to the color of a text display. I made an array of colors and built the index by saying:

which is equivalent to setting bits and treating the result as an unsigned byte from 0..15. I was actually disappointed there was no |= operator.


A good question is never answered. It is not a bolt to be tightened into place but a seed to be planted and to bear more seed toward the hope of greening the landscape of the idea. John Ciardi
Henry Wong
author
Sheriff

Joined: Sep 28, 2004
Posts: 18848
    
  40

I was actually disappointed there was no |= operator.


Really?!? I could have sworn that I have used it before.

.... wait a sec ....

Hmmmmm.... I just went and tried it. And it works. There is no "||=" operator, but "|=" works fine.

Henry
Stan James
(instanceof Sidekick)
Ranch Hand

Joined: Jan 29, 2003
Posts: 8791
Yup ||= was the one I wanted. Must have been a completely different problem, though!
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: applications of bitwise operators