Win a copy of Re-engineering Legacy Software this week in the Refactoring forum
or Docker in Action in the Cloud/Virtualization forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

applications of bitwise operators

 
T Chakradhar Reddy
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 2941
59
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Marshal
Pie
Posts: 20892
75
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Stan James
(instanceof Sidekick)
Ranch Hand
Posts: 8791
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Henry Wong
author
Marshal
Pie
Posts: 20892
75
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 8791
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yup ||= was the one I wanted. Must have been a completely different problem, though!
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic