Numeration Inked
Numeration Inked
COMPSCI 1020
Outline
• Binary numbers
• Operations on binary numbers
Computer Number Systems
• In computers, information is transmitted using
electricity.
• Electricity moving through a wire is represented as 0
(off) or 1 (on).
• Because of this, computers use a number system
that only uses digits 0 and 1.
• Called binary numbers
What do we use?
• Our numeration system is decimal.
• It uses the digits 0-9 to count.
• Numbers larger than 9 are represented using
multiple digits.
– The value of a multi-digit number is calculated
using powers of 10.
Binary numbers
• Only digits are 0 and 1.
– Numbers larger than 1 need multiple digits.
• Next number after 1 is 10.
• Then 11 .. Then what?
• How do we understand binary numbers in terms of
their actual value?
– Decimal numbers were represented using powers
of 10
– Binary numbers are represented using powers of
2.
Sequence of Binary Numbers
• How do sequences of numbers work?
• …88, 89, 90, 91, 92, 93, 94, 95, 96, 97, 98, 99, 100,
101…
• In binary:
Binary numbers
• Sequence is 11, 100, 101, 110, 111, 1000, …
• How do we understand binary numbers in terms of
their actual value?
– Decimal numbers were represented using powers
of 10
– Binary numbers are represented using powers of
2.
Binary Number Example
• Consider 10110. What value does it correspond to?
Terminology for binary numbers
• We refer to “binary digits” as “bits” – one bit is
either 0 or 1.
• We often also talk about “bytes” (8-bit numbers).
• We won’t worry about bytes because they are used
in the context of computer memory.
– 1 TB hard drive = 1 trillion bytes of storage.
Terminology - Base
• We also can talk about the base of a number.
• Decimal is “base 10”
• Binary is “base 2”
Notation for numbers
• Since we need to talk about multiple number types,
we need some notation.
• If X is a string of 0s and 1s (representing a binary
number) then dec(X) is the numerical value of the
string.
• e.g., what is dec(10010) ?
• If n is a decimal number, then bin(n) is the string of
0s and 1s (without leading zeroes) whose value as a
binary number is n
Binary Number: conversions
• How do we convert a number from binary into
decimal?
• How do we convert a number from decimal into
binary?
Binary to Decimal
• We’ve seen this already.
• Use the representation form using exponents.
Binary to Decimal
• Another way to think about conversion: assign a
power of two to each position.
• e.g., convert 1001011001 to decimal.
Decimal to Binary
• To convert a decimal number to binary we will use an
algorithm.
• Need two operations first
Helpful Operation: integer division
• Integer division: x div y = x divided by y with no
fractional part.
– In python, operation is //
– In Java/C/C++/C#, operation is / with two integers.
Helpful Operation: remainder
• Remainder: x mod y = remainder when x is
divided by y. (what’s “left over”)
– Obtained from the grade-school division algorithm
– In python and java, use %.
Decimal to Binary
Input: positive number n (n>0)
Output: the bin(n), the binary number with value n.
1. d = n mod 2
2. n = n div 2
3. next (higher) digit of output is d
4. if n > 0 then go to step 1.
Decimal to Binary Example
• Let’s find the binary value of 119.
Operations on Binary Numbers
• How do we add two binary numbers?
• Can use our standard “grade school” algorithm.
• Just need to remember that 1+0 = 0+1=1 and 1+1=10
in binary 1 1 0 1 1
• 11011 + 1010 = ? + 1 0 1 0
1 1 0 1 0 0 0 1 0 1
Two’s Complement - Positive
• Most significant digit is 0
• Remaining numbers represent a binary number as
we’ve seen.
• To fit in a fixed width, may have additional leading
zeroes.
• e.g., +13 as an 8-bit number.
Two’s Complement - Negative
How do we get two’s complement of a number -X ?
• dec(bee)