How 4<<1<<2 is 32? - bit-manipulation

when
1<<2 : 4
4<<1<<2 should be 4<<4 which is 64
but it is showing 32.
I am new to bit manipulation, please let me know where I am doing wrong.

The expression is evaluated from left to right.
4 << 1 << 2
is equivalent to
(4 << 1) << 2
which is the same as
8 << 2
which equals
32

Related

Integer as an condition in C++'s " :?" operator

So , for my exam I have to evaluate some C++ expressions.
Here s the expression :
float x=3<<2>>1?4.:.5?6:7>8;
Can someone explain it with words because I can t understand it.
So , I have to convert 3 to binary and shift it 2 bits. But After conversion the result is 11. I may add the bit sign so it becomes 011. And after the bit sign I can add as many 0's as i need and so 3<<2-> 01100. But after that I can't understand a thing.. Where s the condition on the conditional operator ?: because I can't see any?
Thank you in advice for helping me solve this out :)
Let's format this a little better:
float x = (3 << 2) >> 1 ? 4. :
.5 ? 6 :
(7 > 8);
Or even better:
float x;
if( 3<<2>>1 ) {
x = 4.;
} else if( .5 ) {
x = 6;
} else {
x = 7 > 8;
}
Walking through the evaluation of 3 << 2 >> 1:
(3 << 2) >> 1 =>
(b0011 << 2) >> 1 =>
b1100 >> 1 =>
b0110 =>
6
Since 6 is a nonzero value (which evaluates to true in C++) x will have the value 4.
The expression is
3<<2>>1?4.:.5?6:7>8
Let's add some spaces and parens:
((3 << 2) >> 1) ? 4. : (.5 ? 6 : (7 > 8))
Shifting by 1 is just multiplying by 2. Shifting by 2 is multiplying by 2, twice.
So 3 << 2 is 12.
12 >> 1 divides by 2, so that's just 6.
As a condition, all integers (except 0) are true, so this simply returns 4.0.

I don't understand SpriteKit physics bodies's categorybitmasks unit

I don't understand UInt32 in spritekit categorybitmask. They are very confusing to me because they are typed like: "0b01 << 1" or "0x01" or something like that. I understand their usage in spritekit, but I don't understand the unit.
You have to understand a little about binary which can be tricky when you first encounter it. The code that is confusing you is shorthand for working with binary.
For example, in 0b01 << 1 the 0b prefix means the following number is given in binary. This is helpful because if you just saw, for example, 1001, you might think it is the number "one thousand and one". But if you write 0b1001 you know that here, 1001 is a binary number. (Read more)
Similarly, 0x is a prefix used to express hexadecimal numbers (because x kinda sounds like "hex" I guess). I personally prefer to use the 0b prefix when dealing with bitmasks in SpriteKit, but that's just me.
Now on to bit shifting. << is the bitwise left shift operator in Swift. It moves all of the bits in a number to the left by a certain number of places. For example, the decimal number 3 is written as 0011 in binary.
3 = 0011 // the number 3 is 0011 in binary
3 << 1 = 0110 = 6 // take 0011 and shift all bits one place to the left
3 << 2 = 1100 = 12 // take 0011 and shift all bits two places to the left
The example you gave was 0b01 << 1. If you just enter 0b01, that is the binary representation of the decimal number 1. By entering 0b01 << 1 you are telling the program to take the binary number 01 (more explicitly written as 0b01) and shift its bits one place to the left, which results in the binary number 10 (more explicitly written as 0b10) , which is the number 2 in decimal.
You can actually write 0b01 as 0b1 for short and it means the same thing since you are just omitting the leading zero.
Try entering the following in an Xcode Playground and look at the results you get:
0b1 // decimal 1
0b1 << 1 // decimal 2
0b1 << 2 // decimal 4
0b1 << 3 // decimal 8
0b1 << 4 // decimal 16
You can play around with this, bit shifting by greater amounts, and you will see that the numbers continue to double. It turns out that this is a handy way for SpriteKit to implement bitmasks for its physics engine.
UPDATE:
You can define up to 32 different categories to use for physics bitmasks. Here they all are with their corresponding decimal values:
0b1 // 1
0b1 << 1 // 2
0b1 << 2 // 4
0b1 << 3 // 8
0b1 << 4 // 16
0b1 << 5 // 32
0b1 << 6 // 64
0b1 << 7 // 128
0b1 << 8 // 256
0b1 << 9 // 512
0b1 << 10 // 1,024
0b1 << 11 // 2,048
0b1 << 12 // 4,096
0b1 << 13 // 8,192
0b1 << 14 // 16,384
0b1 << 15 // 32,768
0b1 << 16 // 65,536
0b1 << 17 // 131,072
0b1 << 18 // 262,144
0b1 << 19 // 524,288
0b1 << 20 // 1,048,576
0b1 << 21 // 2,097,152
0b1 << 22 // 4,194,304
0b1 << 23 // 8,388,608
0b1 << 24 // 16,777,216
0b1 << 25 // 33,554,432
0b1 << 26 // 67,108,864
0b1 << 27 // 134,217,728
0b1 << 28 // 268,435,456
0b1 << 29 // 536,870,912
0b1 << 30 // 1,073,741,824
0b1 << 31 // 2,147,483,648

0 < res <= (1 << 31) -1 - What does this mean?

This statement checks whether a number is 32 bits.
0 < res <= (1 << 31) -1
I can't seem to understand how, can someone help understand this bit shift syntax?
Well, lets begin with an example:
1 in binary is 1
2 in binary is 10
4 in binary is 100
We can see that we need to 'add' an 0 at the end of each number to multiply by 2 and in most language we can do this with this syntax: number << 1
Here we are saying that we add a 1 time a 0 to the left. number >> 1 and here we add 1 time a 0 to the right.
So 1 << 31 means 1 * 2 * 2 * 2 ... 31 times which means 2^31 (so 32 bits)

left shifting in between two different unsinged integer values [duplicate]

This question already has answers here:
Confusing operator precedence: a << b + c << d
(4 answers)
Closed 8 years ago.
when am I applying left shifting to 1 by 4 bit as 1<<4 it printing 16 as the value but if I will apply shifting like 1<<4 + 1<<3 then it printing 256 as a result,
I am not getting, how many shifting is apply and how it is working?
but according to me answer should be 24 by applying 4 left shift + 3 shift
1 << 4 + 1 << 3
Is actually interpreted as
(1 << ( 4 + 1 )) << 3
See?
1 << 5 --> 32
32 << 3 --> 256
When in doubt, use more parentheses!
(1 << 5) + (1 << 3) --> 24

Why does "number & (~(1 << 3))" not work for 0's?

I'm writing a program that exchanges the values of the bits on positions 3, 4 and 5 with bits on positions 24, 25 and 26 of a given 32-bit unsigned integer.
So lets say I use the number 15 and I want to turn the 4th bit into a 0, I'd use...
int number = 15
int newnumber = number & (~(1 << 3));
// output is 7
This makes sense because I'm exchanging the 4th bit from 1 to 0 so 15(1111) becomes 7(0111).
However this wont work the other way round (change a 0 to a 1), Now I know how to achieve exchanging a 0 to a 1 via a different method, but I really want to understand the code in this method.
So why wont it work?
The truth table for x AND y is:
x y Output
-----------
0 0 0
0 1 0
1 0 0
1 1 1
In other words, the output/result will only be 1 if both inputs are 1, which means that you cannot change a bit from 0 to 1 through a bitwise AND. Use a bitwise OR for that (e.g. int newnumber = number | (1 << 3);)
To summarize:
Use & ~(1 << n) to clear bit n.
Use | (1 << n) to set bit n.
To set the fourth bit to 0, you AND it with ~(1 << 3) which is the negation of 1000, or 0111.
By the same reasoning, you can set it to 1 by ORing with 1000.
To toggle it, XOR with 1000.