What are the possible uses of the bitwise operators. i.e. bitwise AND, bitwise OR, bitwise XOR, shift right, shift left and complement.

Also, what are the applications of bitwise operators in everyday problem solving. Examples would be nice. Thanks in advance

## All 12 Replies

That's like asking what the possible uses of arithmetic are and applications for everyday life. Can you narrow down your question just a little?

I'm glad you asked, because when I was a beginning programmer back in CSC100, I wondered the exact same thing.

If your school offers a course in assembly language, it will definitely unlock this mystery, as a lot of operations at the assembly level use bitwise operations.

Here are just a few uses of bitwise operations that I can think of right now:

• some simple encryption techniques use an XOR operation
• you can 'OR' stuff together to get multiple properties (you see this a lot in windows programming). One example is or'ing colors together to get another color.
• you can apply an 'AND' operation as a numerical mask.. one that comes to mind is a subnet mask (if you are familiar with networking)
• you can perform fast multiplication by powers of 2 by shifting bits to the left.
• the idea of 'One's compliment' and 'Two's compliment' are just simple techniques for handling negative (or 'signed') values at the assembly level.
• some data compression techniques (I believe the Huffman algorithm) use bitwise operations.

If you want a simple homework assignment, try writing a program that takes a numerical input from the user, and your program should convert and display the binary equivilant.

A bit can only hold 1 of 2 values (1) or (0). These can be interpreted as (on/off), (yes/no), (true/false), etc.

One common use is to use bit shifts and the bitwise logicals to condense several of these types of values into a single variable to save space.

For example, if you have a game that you can activate and deactivate certain settings/rules on, you can store all of them as bits in the same variable and use less memory.

i understand how and when to use the logical operators, I read about the bitwise operators, but i saw a question that had to do with on and off switches and the solution contained bitwise operators which, from what i have read about it, has to do basically with binary numbers, and not with on and off devices. The question i saw is:

The Snapper is a clever little device that, on one side, plugs its input plug into an output socket, and, on the other side, exposes an output socket for plugging in a light or other device.

When a Snapper is in the ON state and is receiving power from its input plug, then the device connected to its output socket is receiving power as well. When you snap your fingers -- making a clicking sound -- any Snapper receiving power at the time of the snap toggles between the ON and OFF states.

In hopes of destroying the universe by means of a singularity, I have purchased N Snapper devices and chained them together by plugging the first one into a power socket, the second one into the first one, and so on. The light is plugged into the Nth Snapper.

Initially, all the Snappers are in the OFF state, so only the first one is receiving power from the socket, and the light is off. I snap my fingers once, which toggles the first Snapper into the ON state and gives power to the second one. I snap my fingers again, which toggles both Snappers and then promptly cuts power off from the second one, leaving it in the ON state, but with no power. I snap my fingers the third time, which toggles the first Snapper again and gives power to the second one. Now both Snappers are in the ON state, and if my light is plugged into the second Snapper it will be on.

will the light be on if i have N snappers and i snap my fingers K times?

If you ignore certain realities, such as the adapter's reaction time and the grouping's orientation relative to the origin of the sound, and assume that they all instantly toggle at the same instant, then you could probably say that the light would turn on after every [tex](2 ^ n) - 1[/tex] "snaps".

But in reality certain variations such as these would likely cause the light to never turn on.

how was that solution gotten?

Think about the positional values of the individual bits and how counting works in binary.

If you assume that the positions of the bits are numbered from 1 to [tex]N[/tex] and the position of the bit is represented as [tex]P[/tex]. It can be said that the numeric value of the bit is equal to [tex]2^(^P^-^1^)[/tex].

For example, the bit at position 1 has a value of [tex]2^(^1^-^1^)[/tex] which solves out to [tex]2^0 = 1[/tex]. The bit at position 2 has a value of [tex]2^(^2^-^1^)[/tex] which solves out to [tex]2^1 = 2[/tex] etc...

thanks...you just gave me an insight!

Check this site out. It shows you many bit hacking solutions. If you program in a closed, restricted and limited programming hardware, they come to use.

commented: Good post. +0

Check this site out. It shows you many bit hacking solutions.

I logged in just to say a thanks to you.

The use of logical operator is creating logic.
Examples:
1.Finding even number which will be divided by 3.

``````if(number%2==0 && number%3==0)
cout<<number;``````

The output can be....
6,12,18 etc.
6,12,18 are even and divided by 3 also.Both conditions are right here.
Here I use the logical and for checking both conditions.

2.Finding number which can be divided by 6 or 9.

``````if(number%6==0 || number%9==0)
cout<<number;``````

The output can be...
6,9,12,18 etc.
Here d is divided by 6 , but not by 9.
Only one condition needs to be true.

The best example is to get circuit out puts when the connection is based on series or parallel and meant for series and other is the other

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, learning, and sharing knowledge.