This week's book giveaway is in the OO, Patterns, UML and Refactoring forum. We're giving away four copies of Refactoring for Software Design Smells: Managing Technical Debt and have Girish Suryanarayana, Ganesh Samarthyam & Tushar Sharma on-line! See this thread for details.
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.
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.
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