DEV Community

Emily Cain
Emily Cain

Posted on

Bitwise Operations on your Fingers πŸ™ŒπŸ‘©πŸ»β€πŸ’»

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. 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 to represent numbers. Bitwise operators perform a Boolean operation 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)
Enter fullscreen mode Exit fullscreen mode

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) 
Enter fullscreen mode Exit fullscreen mode

XOR (true if the two values are different):

0110 (6) 
0011 (3) 
----
0101 (5) 
Enter fullscreen mode Exit fullscreen mode

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)
Enter fullscreen mode Exit fullscreen mode

You can play with bitwise arithmetic using this 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, or half-byte, which can be represented by a single 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

2, or 0010:

middle finger up, others down

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

middle and index fingers up, others down

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

ring, middle, index fingers up, pinky down

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

pinky and ring fingers up, middle and index down

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

all fingers up

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.

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).

Some more examples:

12 AND 7 = 4

10 AND 5 = 0

OR

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

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

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).

Some more examples:

12 OR 7 = 15

10 OR 5 = 15

8 OR 1 = 9

XOR

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

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).

Some more examples:

12 XOR 7 = 11

10 XOR 5 = 15

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

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.

Top comments (9)

Collapse
 
phlash profile image
Phil Ashby

Brilliant pictures! Fun fact - using four fingers (a 4 bit nibble) as you have gives you a representation of one hexadecimal digit on each hand, so you can easily translate between the commonly written form (hex) and hands. Wanna evaluate (0xff431023 XOR 0x6742a7b2)? Work through the hex nibbles one pair at a time!

it's nibbles all the way down.... ;)

Collapse
 
equiman profile image
Camilo Martinez • Edited

It all make sense now

🀘AND 🀟 = 🀘
1001 AND 1001 = 1001
9 AND 9 = 9

See it upside-down andyou will get: 666. It's a secret invocation πŸ‘Ή

With great power comes great responsability. Use only when need fix bugs in production.

Collapse
 
rebeccacaroline profile image
Rebecca Holzschuh

Thank you for writing and publishing this! This is super helpful

Collapse
 
trueneu profile image
Pavel Gurkov

Next thing to do would be to learn how to shift to the right a two’s complement using fingers.

Collapse
 
elmuerte profile image
Michiel Hendriks

Nice :) And this scales up to the total number of people around you. (Granted they still have all their fingers.)

Some comments may only be visible to logged-in visitors. Sign in to view all comments.