Lol... will do
Main Topics
Browse All TopicsI'm trying to take the next step in programming, and for me that is to learn about bitwise operators. I don't even know what bitwise is, save for the fact that it has to do with doing operations based on individual bits (as the name suggests).
Can someone please give me a starter on what it is (the basics) and where I'll be using it? I mainly do programming in JavaScript and PHP, but I'm about to start learning C++ and Java. I have come across bitwise operators in JavaScript before, but had no idea what they meant or how I could possibly need them. I saw a JS question a few weeks back that dealt with some encryption, so that seems really interesting.
Anyway, throw me a frickin' bone here!
Thanks!
This Question has been solved and asker verified All Experts Exchange premium technology solutions are available to subscription members.
Experts Exchange has been collecting answers to technology questions since 1996…3 million and counting! If you have a question, chances are we already have your answer.
If you can't find the exact answer you're looking for, ask our exclusive community of 50,000 experts. You’ll get a personalized answer from a trusted professional.
Thousands of free tech tips, tricks, how-to’s and tutorials are available in our peer reviewed articles section. See for yourself how smart our experts are, no login required.
Access the answers to your technology questions today.
30-day free trial. Register in 60 seconds.
Members of the expert community talk about why the experience at Experts Exchange is different than what you will find anywhere else.

Try it out and discover for yourself.
30-day free trial. Register in 60 seconds.
Join the community of experts here and help other tech pros by answering question in your area of expertise. You can earn FREE access to all Experts Exchange's premium features and resources.
Ok, so that explains a bit about how to use the operators, but I'm at a loss as to what the purpose of them is. What would be the purpose in bit shifting, inverting or playing with bits altogether? Someone said in that page, if I don't know what bitwise is, I probably don't need it. But that's the thing, I'm trying to explore another aspect of programming.
Also take a look at http://www.cprogramming.co
A bit more in-depth: http://www.codeproject.com
Maybe you'll have a look at http:Q_24814784.html
Well here's an example:
You need to call a method that takes an char as a parameter: it wants a Unicode character encoded as UTF-16, let's say the Euro symbol. You, otoh, since the character takes up two bytes, have it as a byte array since you read it from a Socket.
You therefore need to convert byte[2] to char:
'Bit' is short for 'binary digit'. All values in a computer are stored as a series of ones and zeros. For example, when a variable contains an integer or a character, the value is encoded as bits.
x = 12; // stored as 0000 1100, 1*8 + 1*4 + 0*2 + 0*1 = 12
x = 63; // stored as 0011 1111, 1*32 + 1*16+ 1*8 + 1*4 + 1*2 + 1*1 = 63
x = 'a'; // stored as 0110 0001, arbitrary value assigned by ANSI
When you perform a bitwise operation, you perform an action on the individual bits of the value stored in the variable.
You can perform a bitwise operation on any piece of data, but most commonly, such operations are performed on character data.
Hope that helps.
Ok thanks, @jkr (http:#25721406). That pretty much explains when I'll be using it. I'm interested in looking into the encryption side of programming. I still don't quite understand what shifting a byte stands to accomplish. I'm trying to see the semi-big picture here.
@CEHJ (http:#25721561): Thanks for that example.
>> I'm interested in looking into the encryption side of programming. I still
>> don't quite understand what shifting a byte stands to accomplish.
Well, a maybe-not-too-complex example on how bitwise operations come in handy is the MD5 Algorithm: http://en.wikipedia.org/wi
bitwise operations can perform masking and unmasking of individual bits, thats putting it very mildly. At the most basic level that is where they help out in Encryption.
for instance lets say you have a bit pattern as 101
you want to encrypt it as 111 and only you know the method to do this. to accomplish this you can bitwise OR 101 with 010 and you would get a 111.
On the other side when your encrypted pattern is received you will now have the intel to decrypt it and get the original pattern back.
These operators also come in very useful for device driver routines and low level firmware programming.
take a look at our previous post on this topic
http://www.experts-exchang
also take a look at the following threads
http://stackoverflow.com/q
http://bytes.com/topic/c/a
I'll just give you a quick rundown on bits and why we use these strange "bitwise" operators. If I repeat anything the other experts have said, I apologize, as I don't have time to read all of their responses and adjust mine accordingly.
Quite often, we need to pass 'messages' from one object to another, and those 'messages' contain values that can only mean a set amount of things. For example, when we want to open a file, there are only a few set ways we can do this: write-only, read-only, or read-write (with some more optional stuff, such as whether or not to lock the file, etc). We could make an object that contained 4 boolean variables (one for read, write, read-write, and lock), but remember that a Boolean doesn't actually take up a single bit, as RAM doesn't allow such a layout of bits.
Also quite often, we need to drill messages from one place to another hundreds of times a second, and every bit counts, so we want to compact these messages to make them as lightweight as possible. This is when bit flags come in. We can combine the above 4 bits into a numeric value, and treat them as binary instead of numbers. For example, suppose you are the method who is acting on opening files, and you receive a message like this:
00000110
As you also know, we usually work in bytes (8 bits), so I padded on 5 extra zeros to the left. The 6th bit indicates read access, the 7th indicates write, and the last indicates lock. 0 means false, 1 means true, of course. So, if the 6th bit is on, we need read access. If the 6th AND 7th bits are on, we need read-write, and if the 8th bit is on, we need to lock the file. Easy, right? The above equates to 6 if you convert it from binary into a numeric value (which is what we, as humans, work with usually). There are six bitwise operators in most (if not all) langauges:
Bitwise AND ( & )
Bitwise OR ( | )
Bitwise XOR ( ^ )
Bitwise Left Shift ( << )
Bitwise Right Shift ( >> )
Bitwise COMPLEMENT (Or NOT) ( ~ )
Each of these operators have unique properties, but they all have one thing in common: they operate on binary values. The first five operate on two binary values, while the last has a single operand, and thus only operates on one value. The first operator, Bitwise AND ( & ), will return true, or 1, if two corresponding bits are both true. For example, look at the following:
1
& 1
1
Because those two bits were both true, the AND operation returned another true. However, the operators don't have to work one bit at a time. You can operate on a whole byte, or a whole short (16 bits), a whole 32-bit int, and so on. Usually, you only use this with booleans and non-floating point values. Take this for example (I'm just using 8 bits because I'm lazy, heh):
0 1 1 0 0 1 1 0
& 1 0 1 1 0 1 1 1
0 0 1 0 0 1 1 0
Now that you have an idea as to how bitwise operators work, I'll just explain the rest in general.
Bitwise OR returns true if either of the two bits are true. So, the same two binary values (above) OR'ed instead of AND'ed looks like this:
0 1 1 0 0 1 1 0
| 1 0 1 1 0 1 1 1
1 1 1 1 0 1 1 1
The last of the three in this 'group' (that act using two like binary values) is XOR (Exclusive OR). It's the strangest; it returns true if one of corresponding bits are true, but the other is false. If both are true or both are false, it returns false.
0 1 1 0 0 1 1 0
^ 1 0 1 1 0 1 1 1
1 1 0 1 0 0 0 1
The next two related functions are left shift and right shift. It literally shifts bits to the left or right according to the number of bits. These two act strangely on signed/unsigned numbers, so I suggest searching this on Bing or Google or whatever you like to use.
Complement is the easiest. It just reverses the bits on each bit, so 0 becomes 1 and 1 becomes 0.
So how does this even converge into your programming career?! Well, these operators transform binary values according to certain 'algorithms'. So, let's say you have a set of flags and they mean seperate things. A certain bit being on means one thing, while the next means something else, etc. etc. How do we 'extract' these values and tell if the bits are on or off?! And how do we turn certain bits on and off? Usually, we use AND, OR, and XOR for manipulating flags.
To turn a certain flag off, you can take the value you wish to turn the flag off on and & it by the bit you want to turn off. You have to think in binary, but write code in decimal, so consider the following boolean expression:
(someFlags | 2) > someFlags
Assuming someFlags is a byte (remember, binary is laid out as 128 64 32 16 8 4 2 1 for a byte), when we OR it by 2, if the value gets larger, we know that the 2 bit was off in the original value. If OR'ing it keeps the value the same, we know that the 2 bit was already on.
If you need more help on this, I suggest going through the links other experts have posted. I think my post is quite long enough. However, if you'd like me to elaborate further, I would be happy to :)
Hope it helps,
Nate
Thanks guys. All good answers, but chose these for relevance and content.
I can see a few instances where this might come in handy for working with my databases. Also, I really want to start learning security. I didn't realize bitwise was so important for speeding up the exchange of data. I also never thought about the fact that when manipulating file permissions from my FTP server, I was manipulating the bits! (stored as an integer that is a representation of the 1011 style file attribute system)
Business Accounts
Answer for Membership
by: CEHJPosted on 2009-11-02 at 08:52:30ID: 25721062
Take a look at this. Let me know if you need clarification on any bits(!) of it
ooks/tutor ial/java/n utsandbolt s/ op3.html
http://java.sun.com/docs/b