Bitwise Operations on your Fingers 🙌👩🏻‍💻
Tags: bitwise, logic, binary, math
Published: 2018-11-29 02:06:15

---
title: Bitwise Operations on your Fingers 🙌👩🏻‍💻
published: true
description: learn how to count on your fingers in binary, then use this method to do NOT, AND, OR, and XOR operations.
tags: bitwise, logic, binary, math
---

I first came across bitwise operations as part of an Advent of Code challenge in 2017. I was thoroughly confused. "How can you do AND on two numbers? That doesn't make sense!" I dutifully used the AND operator in whatever language I was using for the challenge (probably Go) and just accepted not knowing what it meant.

My next encounter was in a workplace discussion group where we learned about using AND operators to [filter packets, using `tcpdump`](https://danielmiessler.com/study/tcpdump/). I asked enough questions to actually understand what was going on with these bitwise operations, and I want to share one of the methods I came up with in this article. It involves counting on your fingers in binary and then using your hands to perform bitwise arithmetic -- no computer needed. I don't know if anyone else has done it quite like this, but I find it helpful. 

# Background 

Any computer system you use represents information as a series of 0s and 1s, using base 2 or [binary](https://en.wikipedia.org/wiki/Binary_number#Counting_in_binary) to represent numbers. Bitwise operators perform a [Boolean operation](https://en.wikipedia.org/wiki/Logical_connective) on each bit to reach the final result. 

For example, let's say we want to find 6 AND 3. 

```
0110 (6) 
0011 (3)
----
0010 (2)
```
Breaking this down: 

> In the 2^0 (1's) place, the first value is 0 (false) and the second value is 1 (true). false AND true = false. 
> 
> In the 2^1 (2's) place, the first value is 1 (true) and the second value is 1 (true). true AND true = true. 
> 
> In the 2^2 (4's) place, the first value is 1 (true) and the second value is 0 (false). true AND false = false. 
> 
> In the 2^3 (8's) place, the first value is 0 (false) and the second value is 0 (false). false AND false = false. 

To summarize: for each place or column, do the Boolean logic operation on the input values in that column to get the output for that column. 

You use the same process for doing other bitwise operations. 

OR (true if either value is true):
```
0110 (6) 
0011 (3) 
----
0111 (7) 
```

[XOR](https://en.wikipedia.org/wiki/XOR_gate) (true if the two values are different):

```
0110 (6) 
0011 (3) 
----
0101 (5) 
```

NOT is a unary operator, which is to say it takes only one input, and it reverses the value of each column (or bit). 

```
0101 (5) 
----
1010 (10)
```

You can play with bitwise arithmetic using [this calculator](https://www.miniwebtool.com/bitwise-calculator). 

# Finger Method 

We'll start by learning to finger-count in binary. 

Some finger counting systems start with the thumb, but I am going to use the index finger for 1 because then each hand represents a [nibble](https://en.wikipedia.org/wiki/Nibble), or half-byte, which can be represented by a single [hexadecimal](https://en.wikipedia.org/wiki/Hexadecimal) digit. (It also means I can use my thumb to hold down any unused fingers.) The middle finger is 2, the ring finger is 4, and the pinky is 8.

1, or 0001:

![index finger up, other fingers down](https://thepracticaldev.s3.amazonaws.com/i/23yjnc6958vvos9j1mv3.jpg)

2, or 0010: 

![middle finger up, others down](https://thepracticaldev.s3.amazonaws.com/i/fw5mc6d6zvxfwud6izad.jpg)

3, or 0011, or 2^1 + 2^0: 

![middle and index fingers up, others down](https://thepracticaldev.s3.amazonaws.com/i/odmtkzxtulxipvlt0g7k.jpg)

7, or 0111, or  2^2 + 2^1 + 2^0: 

![ring, middle, index fingers up, pinky down](https://thepracticaldev.s3.amazonaws.com/i/pxvlj355bs4be999756u.jpg)

12, or 1100, or 2^3 + 2^2, or C in hexadecimal:

![pinky and ring fingers up, middle and index down](https://thepracticaldev.s3.amazonaws.com/i/rrj1pudoyb2hls5t4r71.jpg)

15, or 1111, or  2^3 + 2^2 + 2^1 + 2^0, or F in hexadecimal:

![all fingers up](https://thepracticaldev.s3.amazonaws.com/i/6ykoeruwy7pis3rkrxla.jpg)

# AND 

For the AND operation, use your left hand to make the first number and your right hand to make the second number. For each hand, the index finger should always represent 1, the pinky 8. 

Here's 6 on the left hand and 3 on the right. 

![](https://thepracticaldev.s3.amazonaws.com/i/aiuq2zvc550yutzye78x.jpg)

Now take the two hands and place them together, palms touching, fingers tented. 

The fingers that are touching at the top represent the answer (2).

![](https://thepracticaldev.s3.amazonaws.com/i/lya2r4dobtgx5b7wnga2.jpeg)

Some more examples: 

12 AND 7 = 4

![](https://thepracticaldev.s3.amazonaws.com/i/exscr81v6b17hbyztn2s.jpg)

![](https://thepracticaldev.s3.amazonaws.com/i/02xisx298qwrw9kyur2u.jpg)


10 AND 5 = 0 

![](https://thepracticaldev.s3.amazonaws.com/i/rq998udwm07a6ik55sd0.jpg)

![](https://thepracticaldev.s3.amazonaws.com/i/eqwvm6j8aumpbx3oefwt.jpg)

# OR 

Let's use the same inputs with the OR operator. 

Here's 6 on the left hand and 3 on the right. 

![](https://thepracticaldev.s3.amazonaws.com/i/aiuq2zvc550yutzye78x.jpg)

Now take the two hands and place them together, palms touching, fingers tented. 

The places with at least 1 finger up represent the answer (7). 

![](https://thepracticaldev.s3.amazonaws.com/i/y7r0y0qx0xkwethpu66s.jpeg)

Some more examples: 

12 OR 7 = 15

![](https://thepracticaldev.s3.amazonaws.com/i/exscr81v6b17hbyztn2s.jpg)

![](https://thepracticaldev.s3.amazonaws.com/i/02xisx298qwrw9kyur2u.jpg)

10 OR 5 = 15 

![](https://thepracticaldev.s3.amazonaws.com/i/rq998udwm07a6ik55sd0.jpg)

![](https://thepracticaldev.s3.amazonaws.com/i/eqwvm6j8aumpbx3oefwt.jpg)

8 OR 1 = 9 

![](https://thepracticaldev.s3.amazonaws.com/i/zxb9bh9u0ery920nte1r.jpg)

![](https://thepracticaldev.s3.amazonaws.com/i/w6qpm9a996vhwv7u4um8.jpg)


# XOR 


Here's 6 on the left hand and 3 on the right. 

![](https://thepracticaldev.s3.amazonaws.com/i/aiuq2zvc550yutzye78x.jpg)

Now take the two hands and place them together, palms touching, fingers tented. 

The places that have exactly one finger up represent the answer (5). 

![](https://thepracticaldev.s3.amazonaws.com/i/kj3nw0tqwrexv4jgnbxt.jpeg)

Some more examples: 

12 XOR 7 = 11

![](https://thepracticaldev.s3.amazonaws.com/i/exscr81v6b17hbyztn2s.jpg)

![](https://thepracticaldev.s3.amazonaws.com/i/02xisx298qwrw9kyur2u.jpg)

10 XOR 5 = 15 

![](https://thepracticaldev.s3.amazonaws.com/i/rq998udwm07a6ik55sd0.jpg)

![](https://thepracticaldev.s3.amazonaws.com/i/eqwvm6j8aumpbx3oefwt.jpg)


# NOT

Take all the fingers that are up and put them down; put all the fingers that are down and put them up. 

NOT 5 = 10 

![](https://thepracticaldev.s3.amazonaws.com/i/oer0s3b03mir6l0ddj65.jpg)

![](https://thepracticaldev.s3.amazonaws.com/i/q6qls6ngdzkz5c9tpc5k.jpg)


# Conclusion 

Now you know how to do the bitwise operators on your fingers! Hopefully this helps you understand these operations in a more intuitive way.