Was going through documentation (pseudocode) of a particular device. At one point it had:
int b0 = charToInt(*something*)
int Data;
Data = (b0<<4);
What does <<4
mean?
Was going through documentation (pseudocode) of a particular device. At one point it had:
int b0 = charToInt(*something*)
int Data;
Data = (b0<<4);
What does <<4
mean?
That means to do a left shift 4 times on the binary representation of the number.
The number 4 = 0000 0100 in binary.
shifting the bit pattern left four times gives us 0100 0000
4 << 0 = 0000 0100 (what we start with)
4 << 1 = 0000 1000 (8)
4 << 2 = 0001 0000 (16)
4 << 3 = 0010 0000 (32)
4 << 4 = 0100 0000 (64)
0100 0000 = 64
It is a left shift operator, and can be used in this way:
To generate powers of 2 use the following :
1 << 0 == 1
1 << 1 == 2
1 << 2 == 4
1 << 3 == 8
1 << 4 == 16
and so on.
bit shift 4 places to the left, so if b0
was 'A' it would be
'A' = 65 = 0100 0001
bit shifted to the left 4 times is
0100 0001 0000
Note: Ignoring bytes wapping you may or may not have
<< is a bit operation, if I remember correctly. What it does is take the bit value of b0 and shift it over four places. so if b0 = 00000101 b0 << 4 = 01010000
I don't use bitwise operators so I can't really tell you what they're useful for, but hope this helps!
As the other answers have mentioned, this does a bitshift 4 bits to the left. Bit-shifting can be used to quickly multiply or divide by powers of 2. http://en.wikipedia.org/wiki/Bitwise_shift#Bit_shifts
So in this case you are doing:
Why would you want to multiply by 16 or <<4? Well lots of reasons, but one possibility immediately springs to mind - converting a hexadecimal string representation of a number to the number itself. Convert a hexadecimal string to an integer efficiently in C?
The << operator is a bitwise shift. That particular one is a left shift.
Generally these are used in compression algorithms, practical usage in anything else doesn't really exist save very few explicit areas like image pixel setting.