This is an interesting question because the answer... depends LOL
<< LEFT SHIFT
Let's start with the << left shift operator. This shifts the specified quantity left by the specified number of bits, but what you actually end up with depends on the size of the value you are working with.The value you show in your example is 1101010 in binary, but this is only 7-bits wide. Inside the computer we work with values that are 8-bits, 16-bits, or 32-bits wide.
Suppose you are working with an Arduino and you declare a variable as being of type "unsigned int" or uint16_t, which means it is 16-bits wide, and we and set it to the value you suggested (1101010 in binary or 0x6A in hexadecimal), :
uint16_t testValue = 0x006A;
In binary this would be 0000 0000 0110 1010 (note that I'm showing the larger binary values in four-bit groups just to make it visually easier).
If we were to say testValue = testValue << 3, then this means that we shift this value left by three bits. Also, we shift 0s into the least-significant (LS) bits. Meanwhile, the most-significant (MS) three bits "fall off the left-hand side," but since these are all 0s we don't really care, So the result will be:0000 0011 0101 0000 (or 110101000 without leading 0s)Now, the above was based on us working with a 16-bit value -- suppose we were working with an 8-but unsigned value like a byte or a uint8_t as follows:
uint8_t testValue = 0x6A;In binary this would be 0110 1010 (or 1101010 if we avoid leading zeros/0s and omit spaces).
So, what happens if we shift this three bits to the left using testValue =
testValue << 3. Once again, we shift 0s into the least-significant bits, and once again the three most significant bits "fall off the left-hand side," but in this case the three most significant bits of our original 8-bit value are 011, so what we end up with is 01010000NOTE: Shifting a value left by 1 bit is the same as multiplying it by 2. Assume we are dealing with 16-bit values again and we start off with your original value: 0000 0000 0110 1010 = 0x006A (hexadecimal) = 106 (decimal)<< 1 = 0000 0000 1101 0100 = 0x00D4 (hexadecimal) = 212 (decimal)<< 1 = 0000 0001 1010 1000 = 0x01A8 (hexadecimal) = 424 (decimal)<< 1 = 0000 0011 0101 0000 = 0x0350 (hexadecimal) = 848 (decimal) etc.
Of course, this only works if we have enough space to store the result -- suppose we were working with 8-bit unsigned values, in this case we would see the following as bits started "falling off the end":
0110 1010 = 0x006A (hexadecimal) = 106 (decimal)<< 1 = 1101 0100 = 0xD4 (hexadecimal) = 212 (decimal)<< 1 = 1010 1000 = 0xA8 (hexadecimal) = 168 (decimal)<< 1 = 0101 0000 = 0x50 (hexadecimal) = 80 (decimal) etc.
>> RIGHT SHIFT (on your example)
Now let's consider what happens if we perform a right shift. In the case of your example value of 1101010, it's important to remember that this is really 01101010 as an 8-bit value:uint8_t testValue = 0x6A;So if we say testValue =
testValue >> 3, we shift three 0s into the MS bits and the three LS bits "fall off the right-hand side". In this case, the three LS bits are 010, so what we end up with is 00001101
NOTE: Shifting a value right by 1 bit is the same as dividing it by
2. This time, let's assume we are dealing with 8-but values again and we start off with
your original value: 0110 1010 = 0x6A (hexadecimal) = 106 (decimal)>> 1 = 0011 0101 = 0x35 (hexadecimal) = 53 (decimal)>> 1 = 0001 1010 = 0x1A (hexadecimal) = 26 (decimal)>> 1 = 0000 1101 = 0x0D (hexadecimal) = 13 (decimal) etc.
>> RIGHT SHIFT (Logical / on UNSIGNED values)
There is an added twist to all of this -- I think your teacher gave you a 7-bit value of 1101010 so he or she didn't get into the tricky part. If we are dealing with unsigned values, then when we are performing a right shift, we call this a "logical shift" and we always shift 0s into the MS bits. Suppose we start with an UNSIGNED 8-bit binary value of 11110000 (0xF0) and shift it right:uint8_t = 0xF0;
1111 0000 = 0xF0 (hexadecimal) = 240 (decimal)>> 1 = 0111 1000 = 0x78 (hexadecimal) = 120 (decimal)>> 1 = 0011 1100 = 0x3C (hexadecimal) = 60 (decimal)>> 1 = 0001 1110 = 0x1E (hexadecimal) = 30 (decimal) etc.
>> RIGHT SHIFT (Arithmetic / on SIGNED values) However, if we are shifting a SIGNED binary value, then we call this an "arithmetic shift" and we shift copies of the sign bit into the MS bits.
int8_t = 0xF0;
1111 0000 = 0xF0 (hexadecimal) = -16 decimal
>> 1 = 1111 1000 = 0xF8 (hexadecimal) = -8 decimal
>> 1 = 1111 1100 = 0xFC (hexadecimal) = -4 decimal
>> 1 = 1111 1110 = 0xFE (hexadecimal) = -2 (decimal) etc.
I'm sorry... what was the question again?...