Important changes to forums and questions
All forums and questions are now archived. To start a new conversation or read the latest updates go to forums.mbed.com.
9 years, 1 month ago.
Logical Operators Misunderstanding
I've got to ask, tried looking online not much luck, could someone talk through logical operators? like questions in a quiz was find u when u = 5<<2 or u = 9|1 and u=5^5 i get that << is shift in binary but when the numbers get to 100 or 76 how would you figure it out without spending ages looking at the binary values sheet? and also is ^ just arithmetic subtraction? and don't understand too much about | i get that is a NOT and flips the binary value but can someone explain in more detail please?
Question relating to:
2 Answers
9 years, 1 month ago.
See below
Q. I've got to ask, tried looking online not much luck, could someone talk through logical operators? like questions in a quiz was find u when u = 5<<2
Tutors Answer: A left shift by 2 is equivalent to multiply by 4
Q. or u = 9|1 and u=5^5 i get that << is shift in binary but when the numbers get to 100 or 76 how would you figure it out without spending ages looking at the binary values sheet?
Tutors Answer: most calculators can convert numbers to and from binary, including the calculator applications in Windows and OS X. Also, you could write some code to perform the calculations!
and also is ^ just arithmetic subtraction?
Tutors Answer: NO - it is exclusive OR - see the lectures slides
and don't understand too much about | i get that is a NOT and flips the binary value but can someone explain in more detail please?
See one of us in the lab and we can explain.
9 years, 1 month ago.
Okay I will try to explain this as I understand it, When doing a binary shift I try and find the largest power of 2 that will go in. If we use 100 as an example, so if we start with the value 0 which is 0000 0000. The largest power of 2 that goes into this is 64(2^6), so we place that in the column which gives us 0100 0000 and the remaining decimal number is 36, that largest factor of two that will now go into this is 32, so we place that into our binary number and obtain 0110 0000 and a remainder of 4, this then fits nicely so we can achieve 0110 0100. We can then move onto the binary shift. I will use the decimal number 5 with the binary 0000 0101 and do u = 5<<2 we can then shift the binary along. The first iteration would give us 0000 1010 (also shown as u = 10<<1) and then one more iteration gives us 0001 0100 with the value of u = 20. It is basically that you are doing your value multiplied by two to the power of the bitshift. The | is actually an OR if we take your example u = 9|1 we have nibbles of 1001 and 0001. A simple method to show it would be to write the two nibbles above each other and then carry down, in this case it stays as 1001 as there are no new values appearing. Trying this again with u = 9|2 so we have 1001 and 0010 if we were to carry out this operation we would get 1011 . The operation for not is actually ().
EDIT: seems the character for not isn't appearing its the approximate sign, or the squiggly dash