The Artima Developer Community
Sponsored Link

Java Answers Forum
Bit flags vs Boolean

3 replies on 1 page. Most recent reply: Feb 5, 2003 1:46 AM by J.F.Lanting

Welcome Guest
  Sign In

Go back to the topic listing  Back to Topic List Click to reply to this topic  Reply to this Topic Click to search messages in this forum  Search Forum Click for a threaded view of the topic  Threaded View   
Previous Topic   Next Topic
Flat View: This topic has 3 replies on 1 page
Ugo Posada

Posts: 37
Nickname: binaryx
Registered: Dec, 2002

Bit flags vs Boolean Posted: Jan 18, 2003 9:58 AM
Reply to this message Reply
Advertisement
Hello, I was brushing up on Java basics when I came across the shifting operators. Usually I don't give much importance to this because I've never found a good reason to use them. In the Tutorial Sun states that they are widely use to keep state of things, for instance, if a window is resizable, or hidden, whatever.

I've always used boolean variables to keep this kind of info, I guess that one good reason to use it is that in one inetger var you could keep 32 different states by using flags whereas you should create a boolean (8 bits?) for each one of those hence having only 4 possible state variables.

My point of view is that it difficults the readibility of the code for the programmer has to check what are the values of each flag every time he wants to do something, creating ugly sintax.

Is there a good reason to use bit flags, can you give examples of real applications that use this type of syntax.

Best regards.

Ugo Posada
Universidad de los Andes


Tj

Posts: 7
Nickname: tj
Registered: Jan, 2003

Re: Bit flags vs Boolean Posted: Jan 27, 2003 9:49 PM
Reply to this message Reply
> Is there a good reason to use bit flags, can you give
> examples of real applications that use this type of
> syntax.

You should abstract this stuff inside a method. Inside the method, you use bit-shifting operations, but anyone who uses your api doesn't have to know about this.

The use of it is that you don't need to declare a new method for each boolean flag. I hope I'm understanding you correctly!

Matt Gerrans

Posts: 1153
Nickname: matt
Registered: Feb, 2002

Re: Bit flags vs Boolean Posted: Jan 28, 2003 2:05 AM
Reply to this message Reply
Using bitfields in Java is not nearly as popular as it was with C or even C++. These days, with the amount of memory available, guarding your bits so jealously is rarely necessary.

Probably in Java the best reason to use bit-shifting is if you are deciphering some binary format.

J.F.Lanting

Posts: 2
Nickname: sciuriware
Registered: Feb, 2003

Re: Bit flags vs Boolean Posted: Feb 5, 2003 1:46 AM
Reply to this message Reply
There's another reason for using bitfields especially in JAVA. You can group huge amounts of booleans together making your code simpler and even a lot faster. Example:

long status; // Holds 64 booleans and platform independent!
long previousStatus; // The same of yesterday.

if(status == previousStatus) // Check them 64 at once!
{
update(.....
}
The rest of the code might be complex, the testing was swift and simple.
;JOOP!

Flat View: This topic has 3 replies on 1 page
Topic: How do you CSS in Java? Previous Topic   Next Topic Topic: String to Ascii code

Sponsored Links



Google
  Web Artima.com   

Copyright © 1996-2019 Artima, Inc. All Rights Reserved. - Privacy Policy - Terms of Use