Hi all,

I am currently studying for my degree in software development and have just started using java.

In an effort to get ahead of the game I have bought some books on c++ and can happily report I have ubderstood everything up until the part about bitwise opertaors I understand the general reason behing these and how to work out the and/ or calculation but I dont get the not calculation at all can anyone help?

also I dont fully see where these would fit in the real world can anyone give me some example?

I no they allow you give alot of properties to a single work and save memory but just dont see how?

A bit is nothing more than a single "switch" that can be in either an ON or OFF state. An ON bit is represented as a one (1) and an OFF bit is represented by a zero (0).

The Bitwise NOT inverts all bits in the value it's applied to. Any bit that was set to ON is now set to OFF, and vice versa.
For example:

//an 8-bit sequence
00110011
//would become the opposite sequence
11001100
//when subjected to the NOT operator

Edited 5 Years Ago by Fbody: n/a

>> also I dont fully see where these would fit in the real world can anyone give me some example?

Faster calculation, storing boolean variables in a single bit. Too many to mention. Asa personal example, I'm currently working with a wireless protocol. Memory and bandwidth are extremely tight, so I have eight booleans in a single byte. Lets say the most significant bit is whether the device is multi-hop capable. The variable is called "features".

if(features & 0x80)
{
    // code for multi-hop capable
}
else
{
    // code for not multi-hop capable
}

Let's say a variable contains a bunch of dominant genes for a person. First bit is 1 for brown 1 for eyes, 0 for blue eyes, second bit is whatever(my biology isn't too hot. Brown/blue eyes are the only ones I know :))

child = father | mother;

Voila! In one swoop, a child is born!

if(child & 0x80)
{
    // child has brown eyes
}
else
{
    // child has blue eyes
}

The possibilities are endless. You'll find an enormous number of cryptography algorithms using a bit-wise XOR operator.

Vernon covered a lot. This is something that I wanted to share earlier, but I couldn't find it in time.

Imagine you have a program that requires several ON/OFF, TRUE/FALSE, YES/NO values to be stored. It is more efficient to store these values as a single bit than it is to store them as, for example, ints (which are generally 32-bits each). Observe:

//let's store 16 TRUE/FALSE values
//as bits in a bit field or bit set:
0 1 0 1 0 0 1 0 1 1 0 1 0 0 1 0  //<----actual bits
F T F T F F T F T T F T F F T F  //<----associated values

//as ints (same sequence)
00000000000000000000000000000000 //FALSE
00000000000000000000000000000001 //TRUE
00000000000000000000000000000000 //FALSE
00000000000000000000000000000001 //TRUE
00000000000000000000000000000000 //FALSE
00000000000000000000000000000000 //FALSE
00000000000000000000000000000001 //TRUE
00000000000000000000000000000000 //FALSE
00000000000000000000000000000001 //TRUE
00000000000000000000000000000001 //TRUE
00000000000000000000000000000000 //FALSE
00000000000000000000000000000001 //TRUE
00000000000000000000000000000000 //FALSE
00000000000000000000000000000000 //FALSE
00000000000000000000000000000001 //TRUE
00000000000000000000000000000000 //FALSE

Which one is more efficient?

This storage is accomplished by different means ranging from direct bit manipulation using bitwise operators to "syntactic sugar" methods such as bitsets and bit fields. This is an example of a bit field that I designed about a year ago for a game I was working on:

union bitFlags {
	unsigned short flagGroup;  //provide a 16-bit backbone (on 32-bit systems)
	struct {	//the bit field
		signed short multiplierRule  :  2;  //"on" activates the multiples-of-a-kind rule,
                                                    //differing values indicate the rule's active mode
		unsigned short straightRule  :  1;  //"on" activates bonus for a straight in a single roll
		unsigned short threePairRule :  1;  //"on" activates bonus for three pairs in a single roll
		unsigned short hardWinRule   :  1;  //"on" activates the hard win rule-set
		//unsigned short unusedBits    : 11;  //"spare" bits available for future applications
	} ruleFields;	//close the bit field structure
};	//close the union

This particular bit field is 16 bits long and is used to store the settings of 4 different game rules in 5 bits. Notice though that, even though there are 11 "spare" bits, the amount of space used is still less than the 128 bits that would have been used if each individual setting was stored in its own unique int. The individual identifiers give you direct access to the individual bits of the field.

Edited 5 Years Ago by Fbody: n/a

Another example:
You want to know what lights are on in your computerized home.
Bits:
0- Front Porch
1- Entryway
2- Living Room
3- Dining Room
4- Kitchen
5- Bathroom
6- Master Bedroom
7- Kids Bedroom

In the lights byte you have

Byte value  00110101 
Bit numbers 76543210

Which lights are on?

Edited 5 Years Ago by WaltP: Forgot room 5... Gotta have one I guess

Existential question: If room 5 doesn't exist, are the lights on? ;)

Edited 5 Years Ago by Narue: n/a

Comments
Good point...

If room 5 doesn't exist the home may smell rather "funky".

Lighting would be the least of your problems.

Edited 5 Years Ago by frogboy77: n/a

woah guys that has really cleared it up for me thank you so much.

Finally see how that can help with programs and it making alot more sense to me now. At first glance there just seemed no reason for them nut when ou think out the box it becomes alot more obvisous.

I dont think I will attempt to program using bitwise operators at the moment as they seem some what advanced.

But I will definatly be using them in the future hopefully as an employed software developer.

thank you again.

The power of C++ is becoming more apparent to me everyday.

This question has already been answered. Start a new discussion instead.