can any one elaborate me what does flag really means?
Welcome to the Java Programming Forums
The professional, friendly Java community. 21,500 members and growing!
The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.
>> REGISTER NOW TO START POSTING
Members have full access to the forums. Advertisements are removed for registered users.
can any one elaborate me what does flag really means?
oh please change the thread title intoi accidentally typed letter 'f' instead of 's'what is flag?
sori for that.
A flag is basically another word for boolean.
The idea is this:
You can raise a flag, or you can lower it. In it's raised state, the flag is said to be "set", or contains the boolean value true. Likewise, when the flag is lowered the flag is said to be "reset", or contains the boolean value false.
is it "only" for boolean? the book says that its can be also use for any type of data,
is it similar with "initialization"?
Flags usually represent binary data (aka boolean), but can be more complex in their structure (see http://en.wikipedia.org/wiki/Flag_(computing)). To expand on helloworlds explanation, here's a more complex example of using flags: the modifier flag in the InputEvent in the java API
InputEvent (Java 2 Platform SE v1.4.2)
If you don't know what this object is, it essentially contains information about an event (for example this event could be a mouse click). The modifiers variable (accessed via getModifiers()) is an int which contains all sorts of information about other things that happened during the event (such as the shift/alt/control buttons being held down: 1 for yes, 0 for no). But how do you access all these boolean values from an int? Use bitwise operators:
So essentially, all these boolean flags are wrapped into an int and because an int contains 32 bits, it allows you to set 32 different types of flags and pass all this information as a simple integer.if ( (event.getModifiers() & InputEvent.SHIFT_MASK ) != 0 ){ //the shift button was down during the event. }
chronoz13 (November 8th, 2009)
Yes, it can be used for any data type. The concept came from way back in the 1600's and 1700's when ships would raise/lower strings of flags to send messages to other ships (ex.: a white flag denotes they surrender).
Underlying all data in the computer is a ton of bits/flags that are set/reset to denote what a number is.
For example, we can look at a byte as having 8 flags:
(I grouped them into 4 here to make the demonstration easier to see)
0000 0000
The 8 bits represent 8 binary flags (aka. booleans). However, using some math/logic, we can convert them into numbers, and implement a simple adder with only bitwise operators.
the MSB (furthest left bit, or most significant bit) represents how many 2^7's we have.
the second bit from the left represents how many 2^6's we have.
the third bit from the left represents how many 2^5's we have.
...
the LSB (furthest right bit, or least significant bit) represents how many 2^0's we have.
The final number comes from summing up all the numbers we got.
ex:
0010 0011
number = 0 * (2^7) + 0 * (2^6) + 1 * (2^5) + 0 * (2^4) + 0 * (2^3) + 0 * (2^2) + 1 * (2^1) + 1 * (2^0)
number = 1 * (2^4) + 1 * (2^1) + 1*(2^0)
number = 19
A simple method for adding two numbers together bitwise:
(this code was borrowed from here, and was originally formatted to add unsigned long's in C++, I believe. I've changed it to add two bytes in Java, and to make it a little easier to read)
byte add(byte in1, byte in2) { byte bit, carry, tmp; carry = in1 & in2; //Get the carry bits by and'ing the numbers together bit = in1 ^ in2; //Get the answer bits by xor'ing the numbers together while(carry) //Loop until we have rid ourselves of the carry bits { carry = carry << 1; //shift carry bits one so they really will 'carry' tmp = bit ^ carry; //Exclusive OR the carry and answer to get the next answer carry = carry & bit; //Get rid of any carry bits that were used and gain any new ones bit = tmp; } return bit; }
all this, just from a bunch of flags being raised/lowered
Last edited by helloworld922; November 7th, 2009 at 02:17 PM.
chronoz13 (November 8th, 2009)
Id say the word flag in the case of development means a way to hold a status for something. Like for instance, we might be discussing a server/client system and we're designing a user login system with it. When developing we might come across a case where we realise we need be able to tell if a user is an administrator or not, so we decide to put in a "flag" on the user that basically tells us if this user is an admin or not. In this case we'd do it as a boolean, either the user is or isn't an admin. Simple as that.
But in other cases it might be nice to have a flag that can hold three different values or more, in these cases an int or an enum might be nice.
The word "flag" in this case really just means we need to create an instance member of some kind or a static, its a very vague word for what you might need.
// Json
chronoz13 (November 8th, 2009)
ahh so its not just an initialization....
well for now its not easy to understand the real concept of flag.. but ill keep all these information/knowledge you gave me... tnx for all that.....
and one more thing ... im dont have enough knowledge yet with bitwise.... or some bit or byte manipulation