Bitwise State Setting: It Ain't Wise, It's Bitwise

When dealing with OOP, it's quite certain that we'll be facing with boolean states. In most cases, we commonly create a boolean field, and assign then with a getter and setter method. For example, we want to set a form to be visible or hidden by the click of a button. We create a boolean field called visible, and then a getter and setter method for the field.

boolean visible = false;

public void setVisible(boolean visible) {
this.visible = visible;
}

public boolean clickButton() {
if(visible == true) {
// sets the form visible here
} else {
// sets the form hidden here
}
}



Things are just peachy from here, but what happens when we have a lot of boolean states to set here? For instance, we want the form to visible, centered and enabled that just hidden/visible. Well be looking at a lot of boolean fields and probably several getter/setter methods.

boolean visible = false;
boolean centered = false;
boolean enabled = false;

public void setVisible(boolean visible) {
this.visible = visible;
}


public void setCentered(boolean centered) {
this.centered= centered;
}


public void setEnabled(boolean enabled) {
this.enabled= enabled;
}



And if that's not enough, try imagining how these setting are called from another class.

MyForm form = new MyForm();
form.setVisible(true);
form.setCentered(true);
form.setEnabled(true);


As you can see, it's not very pretty. I have to write three lines of code just to set my form's appearance. What happens if I put more states in it? Even worse, what if it was a code for setting the positions for a text string in a form? We could be facing with a long line of codes here, just to set those states.

Fortunately, this is where bitwise state setting comes to the rescue. Instead of setting those states as boolean states, we could alternately set them as a single int state, and then use bitwise operation to set those states. Confused? Let's just try them, by altering the previous codes.

public final static int VISIBLE_STATE = 2; // the states' value must be the power of 2
public
final static int CENTERED_STATE = 4;
public
final static int DEFAULT_STATE = 8;
public int state = 0;

public void setState(int state) {
this.state = state;
}

public void clickButton() {
if((state & VISIBLE_STATE) == VISIBLE_STATE) {
//sets the form as visible
} else {
//sets the form as hidden
}
if((state & CENTERED_STATE) == CENTERED_STATE) {
//sets the form centered
} else {
//sets the form left-aligned
}
if((state & DEFAULT_STATE) == DEFAULT_STATE) {
//sets the form as default
} else {
//sets the form inactive
}
}


Now I'm not going to explain much, you'll probably get more from you mathematics about bitwise operation -- just browse your mathemathic books, or google around. I'll skip to the good stuff. It seems that if you have an integer value by the power of 2 (2,4,8,16,32 etc), joins them with a bitwise OR, then joins them again with the original value with a bitwise AND, the result is the original value itself. For instance, ( 2|4 ) & 2 = 2. This gives us an advantage in our state setting. Have a look how the invocating method is now optimized:

MyForm form = new MyForm();
form.setState(MyForm.VISIBLE_STATE|MyForm.CENTERED_STATE);


Isn't it more convenient that way? Try experimenting more with bitwise operations. If you find something new, or find some problem upon implementation, do let me know ;)
Share on Google Plus

About BhimZ

Aria Bima Mahesa Putra (aka BhimZ) is an experienced software developer, with a lot of experience in various projects with different background. He is currently focused in mobile apps, and has been invested in game development ever since he was a kid in highschool. He founded BhimZ games, his one-man-army game studio as a mean to make his own brand of games during his spare times working as a software engineer in a multinational company. At leisure times he enjoys the company of his family, playing mobile games and watches Japanese tokusatsu and anime with his son.
    Blogger Comment
    Facebook Comment

1 comments:

kaem fujiwara said...

bos bahasa indonesia dunk. tutorial na bahasa inggris mah menit uy...