Numbers (and Characters)
Download
Report
Transcript Numbers (and Characters)
Number Systems
26-Jul-16
Bits and bytes
A bit is a single two-valued quantity: yes or no, true or
false, on or off, high or low, good or bad
One bit can distinguish between two cases:
T, F
Two bits can distinguish between four cases:
TT, TF, FT, FF
Three bits can distinguish between eight cases:
TTT, TTF, TFT, TFF, FTT, FTF, FFT, FFF
In general, n bits can distinguish between 2n cases
A byte is 8 bits, therefore 28 = 256 cases
2
Number systems
The binary (base 2) number system uses two “binary
digits, ” (abbreviation: bits) -- 0 and 1
The octal (base 8) number system uses eight digits:
0, 1, 2, 3, 4, 5, 6, 7
The decimal (base 10) number system uses ten digits:
0, 1, 2, 3, 4, 5, 6, 7, 8, 9
The hexadecimal, or “hex” (base 16) number system
uses sixteen digits:
0, 1, 2, 3, 4, 5, 6, 7, 8, 9, A, B, C, D, E, F
3
Everything is a number?
Everything in the computer is stored as a pattern of bits
Numbers are stored as a pattern of bits
Computers use the binary number system
Characters are stored as a pattern of bits
Binary distinctions are easy for hardware to work with
One byte (8 bits) can represent one of 256 characters
So, is everything in the computer stored as a number?
No it isn’t, it’s stored as a bit pattern
There are many ways to interpret a bit pattern
4
Counting
To count up in any number system,
add 1 to the rightmost digit
if the result is higher than the largest digit,
set that digit to zero and carry to the next place
repeat addition of 1 and carrying as many times as necessary
Example: In hex, F is the largest digit
Hence, 4A6FF + 1 = 4A700
5
Counting in three systems
Dec
0
1
2
3
4
5
6
7
8
9
10
Bin
0
1
10
11
100
101
110
111
1000
1001
1010
Hex
0
1
2
3
4
5
6
7
8
9
A
Dec
Bin
Hex
11
12
13
14
15
16
17
18
19
20
1011
1100
1101
1110
1111
10000
10001
10010
10011
10100
B
C
D
E
F
10
11
12
13
14
6
Computers use binary numbers
People like to use decimal numbers
Computers use binary numbers
Java translates decimal numbers into binary
The computer does all its arithmetic in binary
Java translates binary results back into decimal
You occasionally have to use numbers in other
number systems
In Java, you can write numbers as octal, decimal, or
hexadecimal (but not binary)
Colors are usually specified in hexadecimal notation:
#FF0000, #669966,
7
Using octal and hex numbers
Computers use binary, but the numbers are too long and
confusing for people--it’s easy to lose your place
Octal or hex is better for people
Translation between binary and octal or hex is easy
One octal digit equals three binary digits
101101011100101000001011
5 5 3 4 5 0 1 3
One hexadecimal digit equals four binary digits
101101011100101000001011
B
5
C
A
0
B
8
Writing octal and hex integers
Integers are usually written in decimal notation:
7, 532, -28
To write a number in octal, just start with a zero:
02, 0657, -077
...but don’t use the digits 8 or 9 !
To write a number in hexadecimal, start with 0x or 0X:
0xA, 0X43AB5, -0xFFFF
The “digits” A through F can be upper or lower case
Uppercase is usually preferred
Lowercase is more readable for long numbers
9
ASCII
Up until recently, all programming languages used ASCII
Source: Wikipedia
10
Extended ASCII
In ASCII, the first bit is always zero
Everybody got the bright idea of using a one for the first
bit, to give an additional 128 characters
Everybody added different characters, in different
orders
Lots of people who don't know any better put Extended ASCII
characters (curly quotes, letters with accents marks, etc.) into
their documents
Consequently, other people see these characters as random
garbage
Moral: Don’t use Extended ASCII
11
Unicode
Eventually, ASCII (American Standard Code for
Information Interchange) wasn’t enough
Unicode is the new standard; it has more than 100,000
characters defined
For ease of transition, the first 128 characters are those
defined in the ASCII standard, and require one byte each;
other characters may require up to four bytes
A second Unicode standard uses either two or four bytes for
each character (again, the first 128 are the same as ASCII)
This is the UTF-8 encoding
This is the UTF-16 encoding
Java and all other modern languages now use Unicode
12
The End
“There are 10 kinds of people in the world:
those who understand binary, and those who don’t.”
--Anon.
“Real Programmers always confuse Christmas
and Halloween because Oct31 == Dec25”
--Andrew Rutherford
13