# Bitwise Operators

 0 Hey there,I'm not too savvy on the whole binary ... but I'm learning. I'm beginning to learn about logical bitwise operators like AND operators, OR operators, etc. I know that these are used for boolean expressions with two operands (&, |, &&, ||, etc.), however I'd love to know how, in terms of binary, they actually work. For example, this explanation of the AND operator (here) goes right over my head because of my still limited knowledge of binary.I know what these operators do, I just want to know how they work! :) So basically, could someone please explain how bitwise operators like OR, AND, XOR, etc. work?Also, however, I would love if someone could give me an explanation of bitwise shift operators. And also a context in which it would actually ever be used, if ever. :)Thanks very much,Klep more ▼ asked Apr 22 '12 at 02:36 PM Kleptomaniac 2.5k ● 6 ● 11 ● 21 add new comment (comments are locked) 10|3000 characters needed characters left ▼ Viewable by all users

 0 In assembly programming : ``````ANL A, #00001111B clears four most significant bits. 11001100 Value AND 00001111 Byte used to set the desired value 00001100 ``````See the first four bits (MSB) are cleared while the last four remains the same, you need 1 AND 1 to get 1 ``````ORL A, #00001111B sets four least significant bits. 11001100 Value OR 00001111 byte used to set to the desired value 11001111 ``````The four last bit (LSB) are set to 1 because you need only one 1 to get 1... 1 OR 0 =>10 OR 0 => 0 ``````XRL A, #00001111B inverts four least significant bits. 11001100 Value XOR 00001111 Byte used to set to the desired value 11000011 ``````XOR is the most confusing ``````0 XOR 0 =>0 1 XOR 1 =>0 0 XOR 1 =>1 1 XOR 0 =>1 ``````It is one or the other gives 1 but if both then 0.The big idea being that at processor level, a lot of computation are based on flags, meaning that the processor checks a particular bit inside a byte to define the state. Consider a byte of a router for communication, 00000001 could represent the system is on. 00000011 the system is on and sending information, 10000000 the system is down and so on. You would use AND OR XOR to define the state of the device.For shift operator I think you meant, when a byte is shift to the right or the left. That is used for example in multiplication and division by two. ``````00000001B = 1D shift to the left 00000010B = 2D 00000100B = 4D 00001000B = 8D By shifting I multiplied by 2 `````` more ▼ answered Apr 22 '12 at 03:26 PM fafase 10.5k ● 9 ● 15 ● 41 Wow, thank you for that! That was really helpful. :) Apr 23 '12 at 01:21 PM Kleptomaniac add new comment (comments are locked) 10|3000 characters needed characters left ▼ Viewable by all users
 0 Well, I could write up a big paragraph here. But, it has already been done:http://en.wikipedia.org/wiki/Bitwise_operationEssentially, the computer represents a number with a sequence of bits and bitwise operators manipulate individual bits rather than the entire number to change the number in controlled ways.Bitwise shift operators are quick ways to multiple numbers by powers of 2 (among other things). Bit operations are also used to compare numbers containing a number of "flag" values with just the flag value of interest. more ▼ answered Apr 22 '12 at 02:45 PM AchillesWF 316 ● 3 ● 4 ● 6 Also, you can find out if a number is even with AND'ing it with 1. If the result is 1, than it is even, if the result is 0, than it is odd Apr 22 '12 at 02:54 PM raoz Thanks a lot, doing a bit more research now. :) Apr 22 '12 at 03:09 PM Kleptomaniac add new comment (comments are locked) 10|3000 characters needed characters left ▼ Viewable by all users
 0 A bit string can be an efficient way to represent a lot of bools in a row. Unity uses it mostly for layerMasks. Instead of setting: `layer1=true; layer2=false, layer3=true;` you set the layerMask to 101.You can store up to 32 layer yes/no's in a single int. Even better, the computer's built in hardware can check if the layers match in only 1 step (binary and's, or's ... are built into the chip) instead of using 32 if's.In the old days, you used it everywhere, to save space. Sometimes, if you have two values that never went past 0-15, you used bit-shifting to have them share one 8-bit word. It took longer to read them, but back then 32K was a big computer, so every byte counted. more ▼ answered Apr 22 '12 at 04:52 PM Owen Reynolds 11.3k ● 1 ● 7 ● 45 add new comment (comments are locked) 10|3000 characters needed characters left ▼ Viewable by all users

By Email:

Topics:

x39
x28
x27
x8

asked: Apr 22 '12 at 02:36 PM

Seen: 933 times

Last Updated: Apr 23 '12 at 01:21 PM