# bitshift left (<<), bitshift right (>>)

### Syntax

``` variable << number_of_bits variable >> number_of_bits ```

### Example

```    int a = 5;        // binary: 00000000000000000000000000000101
int b = a << 3;   // binary: 00000000000000000000000000101000, or 40 in decimal
int c = b >> 3;   // binary: 00000000000000000000000000000101, or back to 5 like we started with
```
##### When you shift a value x by y bits (x << y), the leftmost y bits in x are lost, literally shifted out of existence:
```    int a = 5;        // binary: 00000000000000000000000000000101
int b = a << 30;  // binary: 01000000000000000000000000000000 - the first 1 in 101 was discarded
```

##### When you shift x right by y bits (x >> y), and the highest bit in x is a 1, the behavior depends on the exact data type of x. If x is of type int, the highest bit is the sign bit, determining whether x is negative or not, as we have discussed above. In that case, the sign bit is copied into lower bits, for esoteric historical reasons:
```    int x = -16;     // binary: 11111111111111111111111111110000
int y = x >> 3;  // binary: 11111111111111111111111111111110
```
##### This behavior, called sign extension, is often not the behavior you want. Instead, you may wish zeros to be shifted in from the left. It turns out that the right shift rules are different for unsigned int expressions, so you can use a typecast to suppress ones being copied from the left:
```    int x = -16;                   // binary: 11111111111111111111111111110000
int y = (unsigned int)x >> 3;  // binary: 00011111111111111111111111111110
```
##### If you are careful to avoid sign extension, you can use the right-shift operator >> as a way to divide by powers of 2. For example:
```    int x = 1000;
int y = x >> 3;   // integer division of 1000 by 8, causing y = 125.
```

##### Language Reference Home

The text of the 86Duino reference is a modification of the Arduino reference, and is licensed under a Creative Commons Attribution-ShareAlike 3.0 License. Code samples in the reference are released into the public domain.