Page 1 of 1

Scenarios for Bit-Wise Operators Rate Topic: -----

#1 Sheph   User is offline

  • D.I.C Lover
  • member icon

Reputation: 447
  • View blog
  • Posts: 1,032
  • Joined: 12-October 11

Posted 03 June 2012 - 02:31 PM

Scenarios for Bit-Wise Operators

This tutorial assumes that you know the basics behind binary code, bits, and bit-wise operators. You don't need to know what every operator does by heart, but you should understand the fundamentals of bit-wise manipulation and have the ability to look up anything you don't know. If you need to learn these things or just want a refresher, you can check out my tutorial on Understanding Bit-Wise Operators in Java.

Let me say first of all, that often you don't need to use bit-wise operators to accomplish a goal. I'm simply showing some things you can do with the operators. It will be up to you to decide when and if you can apply these operators to your program.

The most common example I can think of for bit-wise operators are Colors. Yep, the pixels on your screen. Look up an RGB chart and you will see that values can range from 0-255 for each red, green and blue. That is only 1 byte of information for each color value. Using bit-wise operators, we can store small blocks of data in a larger block of data. In the case of colors, we can store the four bytes corresponding to red, green, blue, and alpha within 1 single integer. In fact, the Color class in Java internally stores the color value as an integer. The way it works is the left-most byte in the integer is the alpha value which can be from 0-255, the next byte to the right is red, then green, then blue. It looks like this:
aaaa aaaa rrrr rrrr gggg gggg bbbb bbbb
where a, r, g, and b are individual bits that are the value of that part of the color.

So let's create a color using just an integer and some bit-wise operators. First, let's pick the color and write down the RGB values we want. I am going to choose this nice lime green color. The values are, Red=173, Green=255 and Blue=47. The following is a simple GUI that displays a panel of the set color. (Note: the Color class handles all the bit-wise manipulation for you. You would never be obligated to do it yourself like this.)

As an exercise, take a look at the createColor() method and try to understand what the operators are doing. Get out a piece of paper and write yourself a little math problem with the bits. Discover what's happening behind the scenes. (Remember &= or <<= work the same as +=. Just take the operand and add it to the other side. eg, alpha &= 0xff is the same as alpha = alpha & 0xff.)

Bonus Question: Calculate how many pixels there are on your entire screen using your resolution values. Our small window only had 400 x 300. If each of those pixels has to represent an RGBA value (4 bytes), how much memory, in bytes, does that require?

Another use for bit-wise operators would be using individual bits as flags. When I was working in C++, I tended to see this used a lot. You would define a single bit to represent a boolean value or flag. You could set a bunch of these and then | them together to get combinations of flags as 1 parameter. Tell me, if you didn't have to write the class, which constructor would you prefer to use in the main method?

As you can see, we can break all of our Objects / raw types into a series of bytes and using bit-wise operators we could convert them to any other type. We could also design Objects to use the least amount of memory possible. I can fit a TicTacToe board into an integer and still have room in that integer to keep a score for the player. Most people would probably have the score itself as an integer, and an array of chars (or worse Strings) for the board. Though in Java, we don't really overly concern ourselves with memory management. Most of the time, it's better to have understandable code than to have optimally efficient code, but it's a nice skill to know.

Is This A Good Question/Topic? 0
  • +

Replies To: Scenarios for Bit-Wise Operators

#2 pbl   User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8378
  • View blog
  • Posts: 31,956
  • Joined: 06-March 08

Posted 08 June 2012 - 08:06 PM


Another use for bit-wise operators would be using individual bits as flags.

Java has a BiSet Class for that :)
Was This Post Helpful? 2
  • +
  • -

Page 1 of 1