101 Understanding masking and shifting
Tags: octet, bit, java, flags
Published: 2022-06-12 14:26:55

I have interviewed countless IT professionals and am stunned that bits and bytes are foreign words to most of them. I have been with IT for a long time; since we had nothing but assemblers. In today's world, where the costs to buy 16 GB RAM is $50, going to that level may be irrelevant for application servers... but in the world of protocols and IoT, it is very important.

## Primitives

Numerical values can be signed or unsigned. Signed means they have minus values, unsigned means they don't. Java defines 4 types of integer, all signed.

**byte** - 8-bit integer from -128 to 127 (1 byte of storage)
**short** - 16-bit integer from -32,768  to 32,767 (2 bytes of storage)
**int** - 32-bit integer from  -2,147,483,648 to 2,147,483,647 (4 bytes of storage)
**long** - 64-bit integer from -9,223,372,036,854,775,808 to 9,223,372,036,854,775,807 (8 bytes of storage)

Importantly, you can think of a byte as a storage of 8 independent flags, each of which can be turned on (1) or off (0).

If we map the byte (8 bits) in memory, the following is the integer value 0.

<table>
    <thead>
        <tr>
            <th colspan="2">Bits ==&gt; </th>
            <th colspan="2">7</th>
            <th colspan="2">6</th>
            <th colspan="2">5</th>
            <th colspan="2">4</th>
            <th colspan="2">3</th>
            <th colspan="2">2</th>
            <th colspan="2">1</th>
            <th colspan="2">0</th>
        </tr>
    </thead>
    <tbody>
        <tr>
            <td colspan="2">Values (0 or 1) ==&gt;</td>
            <td colspan="2">0</td>
            <td colspan="2">0</td>
            <td colspan="2">0</td>
            <td colspan="2">0</td>
            <td colspan="2">0</td>
            <td colspan="2">0</td>
            <td colspan="2">0</td>
            <td colspan="2">0</td>
        </tr>
        <tr>
            <td colspan="2">Integer Values When 1 ==&gt;</td>
            <td colspan="2">128 (2<sup>7</sup>)</td>
            <td colspan="2">64 (2<sup>6</sup>)</td>
            <td colspan="2">32 (2<sup>5</sup>)</td>
            <td colspan="2">16 (2<sup>4</sup>)</td>
            <td colspan="2">8 (2<sup>3</sup>)</td>
            <td colspan="2">4 (2<sup>2</sup>)</td>
            <td colspan="2">2 (2<sup>1</sup>)</td>
            <td colspan="2">1 (2<sup>0</sup>)</td>
        </tr>
    </tbody>
</table>

As you can see, all combinations between 0 and 255 are possible if they are unsigned. When you form a number, you have to go from the highest bit (most significant) to the lowest (least significant). That is, from bit 7 to bit 0. Take 33, for example. 

Bit 7? No 128 is higher than 33. 
Bit 6? No 64 is higher than 33.
Bit 5? Yes 32 is lower than 33. When you minus 32, we're left with 1 which is the least significant bit of the byte.

<table>
    <thead>
        <tr>
            <th colspan="2">Bits</th>
            <th colspan="2">7</th>
            <th colspan="2">6</th>
            <th colspan="2">5</th>
            <th colspan="2">4</th>
            <th colspan="2">3</th>
            <th colspan="2">2</th>
            <th colspan="2">1</th>
            <th colspan="2">0</th>
        </tr>
    </thead>
    <tbody>
        <tr>
            <td colspan="2">Values</td>
            <td colspan="2">0</td>
            <td colspan="2">0</td>
            <td colspan="2">1</td>
            <td colspan="2">0</td>
            <td colspan="2">0</td>
            <td colspan="2">0</td>
            <td colspan="2">0</td>
            <td colspan="2">1</td>
        </tr>
    </tbody>
</table>

Example for 240

<table>
    <thead>
        <tr>
            <th colspan="2">Bits</th>
            <th colspan="2">7</th>
            <th colspan="2">6</th>
            <th colspan="2">5</th>
            <th colspan="2">4</th>
            <th colspan="2">3</th>
            <th colspan="2">2</th>
            <th colspan="2">1</th>
            <th colspan="2">0</th>
        </tr>
    </thead>
    <tbody>
        <tr>
            <td colspan="2">Values</td>
            <td colspan="2">1</td>
            <td colspan="2">1</td>
            <td colspan="2">1</td>
            <td colspan="2">1</td>
            <td colspan="2">0</td>
            <td colspan="2">0</td>
            <td colspan="2">0</td>
            <td colspan="2">0</td>
        </tr>
    </tbody>
</table>

## Bit Mask

This is all very well, but the aim is to use each bit independently. For example, suppose we want to map a code on a byte (8-bit) that represents a record for a fabric shop:

```
3 Cloth categories

Casual wear (1)
Formal wear (2)
Sports wear (3)

3 Colors

Blue (1)
White (2)
Black (3)

4 Sizes

Small (1)
Medium (2)
Large (3)
XL (4)

```

**Cloth categories** can be kept on 2 bit, since with 2 bit you can make the number 1 (01), 2 (10) or 3 (11). The same can be said for **colors**. To store the **sizes** 2 bit is not enough, you need at least 3: 1 (001), 2 (010), 3 (011) and 4 (100).

Let's now map our byte to store all this information.

<table>
    <thead>
        <tr>
            <th colspan="2">Bits ==&gt; </th>
            <th colspan="2" style="text-align:center">7</th>
            <th colspan="2" style="text-align:center">6</th>
            <th colspan="2" style="text-align:center">5</th>
            <th colspan="2" style="text-align:center">4</th>
            <th colspan="2" style="text-align:center">3</th>
            <th colspan="2" style="text-align:center">2</th>
            <th colspan="2" style="text-align:center">1</th>
            <th colspan="2" style="text-align:center">0</th>
        </tr>
    </thead>
    <tbody>
        <tr>
            <td colspan="2">Values</td>
            <td colspan="2">_UNUSED_</td>
            <td colspan="6">Size</td>
            <td colspan="4">Color</td>
            <td colspan="4">Cloth category</td>
        </tr>
    </tbody>
</table>

So now let's code a casual wear (01), black (11) and size XL (100) => This gives a value of 77 (see map below)

<table>
    <thead>
        <tr>
            <th colspan="2" style="text-align:center">7</th>
            <th colspan="2" style="text-align:center">6</th>
            <th colspan="2" style="text-align:center">5</th>
            <th colspan="2" style="text-align:center">4</th>
            <th colspan="2" style="text-align:center">3</th>
            <th colspan="2" style="text-align:center">2</th>
            <th colspan="2" style="text-align:center">1</th>
            <th colspan="2" style="text-align:center">0</th>
        </tr>
    </thead>
    <tbody>
        <tr>
            <td colspan="2">0</td>
            <td colspan="2">1</td>
            <td colspan="2">0</td>
            <td colspan="2">0</td>
            <td colspan="2">1</td>
            <td colspan="2">1</td>
            <td colspan="2">0</td>
            <td colspan="2">1</td>
        </tr>
    </tbody>
</table>

## How to get the bits in Java

Now that we have a number, we need to look at it as bit map. How do we isolate the bit 0 and 1 (category), bit 2 and 3 (color) and bit 4, 5 and 6 (size)? We can do it my masking and shifting. First, we **mask** to retrieve the bits in the byte, using the **&** operator. The mask is all of the bits, that we need from the byte, set to 1.

```
// Masking the category
int category = mask & 3
```

<table>
    <thead>
        <tr>
            <th colspan="2" style="text-align:center"></th>
            <th colspan="2" style="text-align:center">7</th>
            <th colspan="2" style="text-align:center">6</th>
            <th colspan="2" style="text-align:center">5</th>
            <th colspan="2" style="text-align:center">4</th>
            <th colspan="2" style="text-align:center">3</th>
            <th colspan="2" style="text-align:center">2</th>
            <th colspan="2" style="text-align:center">1</th>
            <th colspan="2" style="text-align:center">0</th>
        </tr>
    </thead>
    <tbody>
        <tr>
            <td colspan="2"><b>category</b></td>
            <td colspan="2">0</td>
            <td colspan="2">1</td>
            <td colspan="2">0</td>
            <td colspan="2">0</td>
            <td colspan="2">1</td>
            <td colspan="2">1</td>
            <td colspan="2"><b>0</b></td>
            <td colspan="2"><b>1</b></td>
        </tr>
        <tr>
            <td colspan="2"><b>mask</b></td>
            <td colspan="2">0</td>
            <td colspan="2">0</td>
            <td colspan="2">0</td>
            <td colspan="2">0</td>
            <td colspan="2">0</td>
            <td colspan="2">0</td>
            <td colspan="2"><b>1</b></td>
            <td colspan="2"><b>1</b></td>
        </tr>
    </tbody>
</table>

```
// Masking the color
int color = mask & 12
```
<table>
    <thead>
        <tr>
            <th colspan="2" style="text-align:center"></th>
            <th colspan="2" style="text-align:center">7</th>
            <th colspan="2" style="text-align:center">6</th>
            <th colspan="2" style="text-align:center">5</th>
            <th colspan="2" style="text-align:center">4</th>
            <th colspan="2" style="text-align:center">3</th>
            <th colspan="2" style="text-align:center">2</th>
            <th colspan="2" style="text-align:center">1</th>
            <th colspan="2" style="text-align:center">0</th>
        </tr>
    </thead>
    <tbody>
        <tr>
            <td colspan="2"><b>category</b></td>
            <td colspan="2">0</td>
            <td colspan="2">1</td>
            <td colspan="2">0</td>
            <td colspan="2">0</td>
            <td colspan="2"><b>1</b></td>
            <td colspan="2"><b>1</b></td>
            <td colspan="2">0</td>
            <td colspan="2">1</td>
        </tr>
        <tr>
            <td colspan="2"><b>mask</b></td>
            <td colspan="2">0</td>
            <td colspan="2">0</td>
            <td colspan="2">0</td>
            <td colspan="2">0</td>
            <td colspan="2"><b>1</b></td>
            <td colspan="2"><b>1</b></td>
            <td colspan="2">0</b></td>
            <td colspan="2">0</td>
        </tr>
    </tbody>
</table>

```
// Masking the size
int size = mask & 112
```
<table>
    <thead>
        <tr>
            <th colspan="2" style="text-align:center"></th>
            <th colspan="2" style="text-align:center">7</th>
            <th colspan="2" style="text-align:center">6</th>
            <th colspan="2" style="text-align:center">5</th>
            <th colspan="2" style="text-align:center">4</th>
            <th colspan="2" style="text-align:center">3</th>
            <th colspan="2" style="text-align:center">2</th>
            <th colspan="2" style="text-align:center">1</th>
            <th colspan="2" style="text-align:center">0</th>
        </tr>
    </thead>
    <tbody>
        <tr>
            <td colspan="2"><b>category</b></td>
            <td colspan="2">0</td>
            <td colspan="2"><b>1</b></td>
            <td colspan="2"><b>0</b></td>
            <td colspan="2"><b>0</b></td>
            <td colspan="2">1</td>
            <td colspan="2">1</td>
            <td colspan="2">0</td>
            <td colspan="2">1</td>
        </tr>
        <tr>
            <td colspan="2"><b>mask</b></td>
            <td colspan="2">0</td>
            <td colspan="2"><b>1</b></td>
            <td colspan="2"><b>1</b></td>
            <td colspan="2"><b>1</b></td>
            <td colspan="2">0</td>
            <td colspan="2">0</td>
            <td colspan="2">0</b></td>
            <td colspan="2">0</td>
        </tr>
    </tbody>
</table>


Then we shift the bits to the right to get the value we want, using the **>>** (shift right) operator.  

```
// Masking the category
int category = (mask & 3) 
```
No need to shift **category**, it's already on the least significant part of the byte.

```
// Masking the color
int color = (mask & 12) >> 2
```

<table>
    <thead>
        <tr>
            <th colspan="2" style="text-align:center"></th>
            <th colspan="2" style="text-align:center">7</th>
            <th colspan="2" style="text-align:center">6</th>
            <th colspan="2" style="text-align:center">5</th>
            <th colspan="2" style="text-align:center">4</th>
            <th colspan="2" style="text-align:center">3</th>
            <th colspan="2" style="text-align:center">2</th>
            <th colspan="2" style="text-align:center">1</th>
            <th colspan="2" style="text-align:center">0</th>
        </tr>
    </thead>
    <tbody>
        <tr>
            <td colspan="2"><b>Color</b></td>
            <td colspan="2">0</td>
            <td colspan="2">0</td>
            <td colspan="2">0</td>
            <td colspan="2">0</td>
            <td colspan="4">==&gt; 2</td>
            <td colspan="2"><b>1</b></td>
            <td colspan="2"><b>1</b></td>
        </tr>
    </tbody>
</table>


```
// Masking the size
int size = (mask & 112) >> 4
```

<table>
    <thead>
        <tr>
            <th colspan="2" style="text-align:center"></th>
            <th colspan="2" style="text-align:center">7</th>
            <th colspan="2" style="text-align:center">6</th>
            <th colspan="2" style="text-align:center">5</th>
            <th colspan="2" style="text-align:center">4</th>
            <th colspan="2" style="text-align:center">3</th>
            <th colspan="2" style="text-align:center">2</th>
            <th colspan="2" style="text-align:center">1</th>
            <th colspan="2" style="text-align:center">0</th>
        </tr>
    </thead>
    <tbody>
        <tr>
            <td colspan="2"><b>Size</b></td>
            <td colspan="2">0</td>
            <td colspan="8">==&gt; 4</td>
            <td colspan="2"><b>1</b></td>
            <td colspan="2"><b>0</b></td>
            <td colspan="2"><b>0</b></td>
        </tr>
    </tbody>
</table>

## In code

```Java
package ans.examples;

public class BitsAndBytes {
    public static final int WEAR_CASUAL = 1;
    public static final int WEAR_FORMAL = 2;
    public static final int WEAR_SPORT = 3;

    public static final int COLOR_BLUE = 1;
    public static final int COLOR_WHITE = 2;
    public static final int COLOR_BLACK = 3;

    public static final int SIZE_SMALL = 1;
    public static final int SIZE_MEDIUM = 2;
    public static final int SIZE_LARGE = 3;
    public static final int SIZE_XL = 4;

    public static byte makeCode(int wear, int color, int size) {
        return (byte)(wear + (color << 2) + (size << 4));
    }

    public static int getWear(byte code) {
        return code & 3;
    }

    public static int getColor(byte code) {
        return (code & 12) >> 2;
    }

    public static int getSize(byte code) {
        return (code & 112) >> 4;
    }

    public static void main(String[] args) {
        byte code = makeCode(WEAR_CASUAL, COLOR_BLACK, SIZE_XL);
        System.out.println("Causal Wear, Black, Size XL = " + code);

        switch (getWear(code)) {
            case WEAR_CASUAL:
                System.out.println("Casual wear");
                break;

            case WEAR_FORMAL:
                System.out.println("Formal wear");
                break;

            case WEAR_SPORT:
                System.out.println("Sport wear");
                break;

            default:
                System.out.println("Invalid wear code");
                break;
        }

        switch (getColor(code)) {
            case COLOR_BLUE:
                System.out.println("Color Blue");
                break;

            case COLOR_WHITE:
                System.out.println("Color White");
                break;

            case COLOR_BLACK:
                System.out.println("Color Black");
                break;

            default:
                System.out.println("Invalid color code");
                break;
        }

        switch (getSize(code)) {
            case SIZE_SMALL:
                System.out.println("Size Small");
                break;

            case SIZE_MEDIUM:
                System.out.println("Size Medium");
                break;

            case SIZE_LARGE:
                System.out.println("Size large");
                break;

            case SIZE_XL:
                System.out.println("Size XL");
                break;

            default:
                System.out.println("Invalid size code");
                break;
        }
    }
}
```