The left shift operator << causes the bits of the left operand to be shifted left by the number of positions specified by the right operand.
variable << number_of_bits;
- variable: Allowed data types: byte, int, long.
- number_of_bits: a number that is < = 32. Allowed data types: int.
※ NOTES AND WARNINGS:
When you shift a value x by y bits (x << y), the leftmost y bits in x are lost, literally shifted out of existence:
If you are certain that none of the ones in a value are being shifted into oblivion, a simple way to think of the left-shift operator is that it multiplies the left operand by 2 raised to the right operand power. For example, to generate powers of 2, the following expressions can be employed:
The following example can be used to print out the value of a received byte to the serial monitor, using the left shift operator to move along the byte from bottom(LSB) to top (MSB), and print out its Binary value:
- Language : >> (bitshift right)
- Language : & (bitwise and)
- Language : ~ (bitwise not)
- Language : | (bitwise or)
- Language : ^ (bitwise xor)
- Example : BitMath Tutorial
※ ARDUINO BUY RECOMMENDATION
|Arduino UNO R3|
|Arduino Starter Kit|