Skip to content

Instantly share code, notes, and snippets.

@normoes
Last active March 14, 2018 09:07
Show Gist options
  • Save normoes/dd916b739a7a64be08be79834b3d928a to your computer and use it in GitHub Desktop.
Save normoes/dd916b739a7a64be08be79834b3d928a to your computer and use it in GitHub Desktop.
remember me: binary math

binary math

source

2's complement

101 -> 010 + 1 = 011
11010 -> 00101 + 1 = 00110

binary subtraction

0 - 0 = 0
1 - 0 = 1
1 - 1 = 0
0 - 1 = 1 with a borrow of 1
 111
- 10
-----
=101
 1001
- 101
-----
=0100

binary subtraction using the 2's complement

 111
- 10
-----
 111
+110 (2's complement of 010 -> 101 + 1 = 110)
-----
1101
dropping carry-over
=101

If there is a carry-over it is dropped.

Without carry-over the 2's complement is built from the resulting value. This resultis negative then.

 10110 
–11010
-----
 10110 
–00110 (2's complement of 11010 -> 00101 + 1 = 00110)
-----
 11100
no carry-over, build 2's complement (is negative result value)
 11100 -> 00011 + 1 = 00100
= -100

binary addition

0 + 0 = 0
0 + 1 = 1
1 + 0 = 1
1 + 1 = 0 with a carry-over of 1
  10
+ 11
-----
=101
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment