Binary number system

Binary number system 

The modern binary number system is usually attributed to Leibniz (1646-1716) but its antecedent was known as far back as the origin of the  I-Ching.  It is a base 2 system whose “alphabet” is denoted by two symbols 0, 1.  Numbers are represented by proper placement.

({{Decimal, Binary, Expansion}, {0, 0000, 0}, {1, 0001, (2^0)}, {2, 0010,  1×(2^1)+ 0 × (2^0)}, {3, 0011, 1 × (2^1)+ 1×(2^0)}, {4, 0100, 1×(2^2)+0 × (2^1)+ 0×(2^0)}, {5, 0101, 1×(2^2)+0 × (2^1)+ 1×(2^0)}, {6, 0110, 1×(2^2)+1 × (2^1)+ 0×(2^0)}, {7, 0111, 1×(2^2)+1 × (2^1)+ 1×(2^0)}, {8, 1000, 1×(2^3)+0 × (2^2)+ 0×(2^1)+ 0×(2^0)}, {9, 1001, 1×(2^3)+0 × (2^2)+ 0×(2^1)+ 0×(2^0)}})

Find the binary representation of the following integers (Toggle for the answers)

16

10000

17

10001

101

1100101

Fractions work in a similar, but more complicated, manner

({{Decimal , Binary , Expansion}, {1/1,  0.1111..., 1/2 +1/4+1/8+1/16 + ...}, {1/2, 0.1000      , 1/2+0 ×1/4+0×1/8+0/16+...
}, {0.625, 0.101  , 1/2+0 ×1/4+1 × 1/8}})

Luckily, Mathematica features expressions that can convert binary numbers to decimal form, and vice versa.
The commands for those functions are

?BaseForm

So if we want to convert the decimal form 13.625 to a binary number, all we need to do is call

BaseForm[13.625,2]

Likewise, given the binary number  13.652 we could use the command

?RealDigits

RealDigits[1101.101]

which can be converted to a base 10 number as a rational fraction using

?FromDigits

FromDigits[RealDigits[1101.1101],2]

Exercises

Construct a function that uses the Mathematica functions described above to express a base 2 number into its base 10 form

base10[base2_]:=N[FromDigits[RealDigits[base2],2]];

Use that function to express the following in base 10 decimal form

100010.01101

base10[100010.01101]

100101.00000

base10[100101.0]

Adding binary integers

When adding binary numbers, we use the following rules
0+0=0
0+1=1
1+0=1
1+1=0 with a carry bit value 1

For example, using these rules

10001101
+ 00001111

is equal to 10011100, and which we check below

FromDigits[{1,0,0,0,1,1,0,1},2]+FromDigits[{0,0,0,0,1,1,1,1},2]==FromDigits[{1,0,0,1,1,1,0,0},2]

Often we use the circle-plus operator ⊕ to carry out the so-called modular sum of two binary numbers. The rules
for it are the same as the plus ÷ operator, except for the fact there is no carry bit, i.e.

0⊕0=0
0⊕1=1
1⊕0=1
1⊕1=0

Thus

10001101
⊕ 00001111

equates to the value 10000010

Mathematica has a command for this operator, also called the Exclusive OR operator,

?BitXor

So

BitXor[{1,0,0,0,1,1,0,1},{0,0,0,0,1,1,1,1}]