Quote:
Originally Posted by shdwsclan
I am native to various languages but bitwise operators just kill me. I see how much I take object oriented languages for granted. I like all the other c derivitives but ANSI C is making me loose my hair....especially ANSI C's bitwise operators.....
For reference i come from the (Java/C#/C++) realm and was never forced to use these.
Many people dont understand these....I tried to make sense....I know the truth tables...and I can do simple operation....can anyone provide a couple answers and possibly give me some explainations on how to attack these...any special tricks on attacking these....I am familiar with these operators but these are tricky and none of my friends got these....I am the most advanced since I at least got 3 correctly..........
:confused: :( :mad: :eek:
Any help is much appreciated.
Just to let you know, these bitwise operators are available in all the languages you described. Perhaps you should reorient your thinking. Think about the integer number as the object and the operators as functions that you invoke on the object.
You really don’t explain what it is that you are having trouble on. Banfa’s explanation is fairly clear. But in case you are still having difficulty with it, most of the bitwise operators operate as if you have a series of Booleans and you are using the same Boolean (a.k.a. logical) operator on all of them in parallel.
So the bitwise or is equivalent to a logical or, the bitwise and is equivalent to the logical and, the bitwise not is equivalent to the logical not when relating the bits in the same place of each number to the other. There is no logical equivalent to the bitwise xor, you would have to do something like this: (a != 0)^(b != 0) which
may be slightly more efficient than (a && !b)||(!a && b) in certain circumstances and less so in others due to the short-circuiting nature of the logical and and or operators.
The left shift and right shift operators shift the bits as you would read them as binary. The number 1 shifted left one would give you 2 as the binary of 1 is 00000001 and the binary of 2 is 00000010. As you will notice, the least significant bit is filled in with a zero (0).
Shifting right is a little trickier as it does something different if you are shifting a signed or unsigned value. If the number you are shifting is signed, then sign extension takes place, so it is filled in with whatever was there to start with. This means that if the most significant bit is 1 then that 1 stays 1 after the shift, if it is 0 then it stays 0. So a signed byte of 10000000 shift right would be 11000000. An unsigned value however would fill the most significant bit with 0 no matter what it was to start with. So an unsigned byte of 10000000 shifted right 1 would result in 01000000. Any bits that fall off the end will be lost forever. There is no access to the overflow like there is in assembly.
In Java, to reduce problems, they added a >>> operator which means that it is to treat the number being shifted
always as an unsigned number, and
I think if you were to use the >> it would treat the number being shifted always as a signed number.
As for + being a bitwise operator, well, that is somewhat on the fence. Like the right shift operator, it has some dependencies related to the state of the operands, though it is slightly more complicated and relates to all but the least significant bit. I can see both sides of the issue, so I have no real qualms about it being considered a bitwise operator.
If you are still having trouble, feel free to post back any additional specific questions that you may have.
Adrian