Jump to content

Introduction to Math for computer programming

From Wikiversity

This page is currently only a very very small fraction of what I intend it to be. If you have idea for topics or the heavenly inspiration to contribute something great to this topic: Please click that "edit" button and do it!

Please try to keep this simple and easy to understand without prior knowledge of anything beyond introductory algebra unless the topic warrants it. If you know calculus: Good for you. That doesn't mean the average viewer of this page does.

Number systems

[edit | edit source]

The following is a list of common number systems. The names of which are derived from latin.

Base Name Number set Commonly used for
1 Unary 1 Tallying (*)
2 Binary 01 Computers (**)
3 Ternary 012
4 Quaternary 0123
5 Quinary 01234
6 Senary 012345
7 Septenary 0123456
8 Octal 01234567 Computers (***)
9 Nonary 012345678
10 Decimal 0123456789 Ordinary Counting!
11 Undenary 0123456789A
12 Duodecimal 0123456789AB
16 Hexadecimal 0123456789ABCDEF Computers (****)
20 Vigesimal Too long!
60 Sexagesimal Too long! Clocks
  • Unary is of very limited usefulness since only positive integers can be expressed in it. The same is more-or-less true for any numbering system whose absolute value is equal to one.
  • Binary is used to portray the specific values of on (1) and off (0) used in digital electronics like computers
  • Octal was more commonly used in the early days of computing and especially on computers whose byte size was divisible by three. It is still used in some computing cultures and for some specific purposes but its use has been largely supplanted by hexadecimal.
  • Hexadecimal is often used to portray binary numbers in a smaller, neater-seeming form

Determining number value

[edit | edit source]

Because people tend to be most familiar with dealing with numbers in Decimal, that is the basis we will use to get a feel for a number's value.

You may determine the decimal value of a number in any given base using the following formula:

Where:

  • K = Decimal value of a given number
  • d = Decimal value of a value in a specified base
  • B = Decimal value of the base of the number being evaluated
  • i = Decimal value of the index at which the number currently being evaluated is located (0 = right-most; 1, 2, 3, etc. are 1, 2, 3, etc. places from the right-most)

In this scheme, the right-most digit in any number in any base is:

  • The digit place with the smallest potential value
  • Has an index (i) value of 0

It is common to determine the value of a number, starting with the right-most value. That's what I'm going to be doing in the following examples.

Converting decimal to decimal

[edit | edit source]

Converting the decimal number 931 to decimal:

1 * 10^0 =   1
3 * 10^1 =  30
9 * 10^2 = 900

900 + 30 + 1 = 931

Converting the decimal number 23 to decimal:

3 * 10^0 =  3
2 * 10^1 = 20

20 + 3 = 23

Converting binary to decimal

[edit | edit source]

Converting the binary number 0b01001100 to decimal:

0b00000000 = 0 * 2^0 =  0
0b00000000 = 0 * 2^1 =  0
0b00000100 = 1 * 2^2 =  4
0b00001000 = 1 * 2^3 =  8
0b00000000 = 0 * 2^4 =  0
0b00000000 = 0 * 2^5 =  0
0b01000000 = 1 * 2^6 = 64
0b00000000 = 0 * 2^7 =  0

64 + 8 + 4 = 76

Converting the binary number 0b11111111 to decimal:

0b00000001 = 1 * 2^0 =   1
0b00000010 = 1 * 2^1 =   2
0b00000100 = 1 * 2^2 =   4
0b00001000 = 1 * 2^3 =   8
0b00010000 = 1 * 2^4 =  16
0b00100000 = 1 * 2^5 =  32
0b01000000 = 1 * 2^6 =  64
0b10000000 = 1 * 2^7 = 128

128 + 64 + 32 + 16 + 8 + 4 + 2 + 1 = 255

Converting hexadecimal to decimal

[edit | edit source]

Converting the hexadecimal number 0x16 to decimal:

6 * 16^0 = 6
1 * 16^1 = 16

16 + 6 = 22

Converting the hexadecimal number 0xBEEF to decimal:

F = 15 * 16^0 =     15
E = 14 * 16^1 =    224
E = 14 * 16^2 =  3,584
B = 11 * 16^3 = 45,056

45,056 + 3,584 + 224 + 15 = 48,879

Decimal

[edit | edit source]
Place name Decimal Value Scientific notation
Millions 1,000,000 106
Hundred-thousands 100,000 105
Ten-thousands 10,000 104
Thousands 1,000 103
Hundreds 100 102
Tens 10 101
Ones 1 100


106 + 105 + 104 + 103 + 102 + 101 + 100 = Expressed as = Decimal value
1 2 3 4 5 6 7 1234567 1234567
0 0 0 0 0 0 0 0 0
4 3 7 5 9 1 0 4,375,910 4,375,910
0 0 0 1 0 2 4 1,024 1,024
9 9 9 9 9 9 9 9,999,999 9,999,999

Binary

[edit | edit source]

Binary relys on only two digits which are usually written 0 and 1, but are easilly encoded as "off" and "on" in switch circuits, which makes Binary the language of choice in low-level Computing and Digital Electronics.


Place name Decimal Value Scientific notation
One-twenty-eights 128 27
Sixty-fours 64 26
Thirty-twos 32 25
Sixteens 16 24
Eights 8 23
Fours 4 22
Twos 2 21
Ones 1 20


27 + 26 + 25 + 24 + 23 + 22 + 21 + 20 = Expressed as = Decimal value
0 0 0 0 0 0 0 0 0b00000000 0
0 1 0 0 0 0 0 1 0b01000001 65
1 0 0 0 0 0 0 0 0b10000000 128
1 0 0 1 1 1 1 1 0b10011111 159
1 1 1 1 1 1 1 1 0b11111111 255

Hexadecimal

[edit | edit source]

Hexadecimal is commonly used to represent binary data, as it occupies only one quarter of the space when written down.


Place name Decimal Value Scientific notation
No thanks! 16,777,216 166
No thanks! 1,048,576 165
No thanks! 65,536 164
40-96s 4,096 163
256s 256 162
Sixteens 16 161
Ones 1 160


167 + 166 + 165 + 164 + 163 + 162 + 161 + 160 = Expressed as = Decimal value
0 0 0 0 0 0 0 0 0x00000000 0
0 0 0 0 0 0 4 1 0x41 65
D E A D B E E F 0xDEADBEEF 3,735,928,559
0 0 0 0 0 F 7 3 0xF73 3,955
F F F F F F F F 0xFFFFFFFF 4,294,967,295

Corollary

[edit | edit source]

In all number systems, prefixed zeros can be ignored (do not affect the value represented), however common practice is to prefix all octal numbers with a leading 0 (zero), all hexadecimal values with 0x and all binary numbers with zeros so that it contains a number of digits which is divisible by eight.

Decimal Value Binary Value Hexadecimal Value
000 00000000 00
001 00000001 01
002 00000010 02
003 00000011 03
004 00000100 04
005 00000101 05
006 00000110 06
007 00000111 07
008 00001000 08
009 00001001 09
010 00001010 0A
011 00001011 0B
012 00001100 0C
013 00001101 0D
014 00001110 0E
015 00001111 0F
016 00010000 10
017 00010001 11
018 00010010 12
019 00010011 13
020 00010100 14
021 00010101 15
022 00010110 16
023 00010111 17
024 00011000 18
--- -------- --
254 11111110 FE
255 11111111 FF