Send
Close Add comments:
(status displays here)
Got it! This site uses cookies. You consent to this by clicking on "Got it!" or by continuing to use this website.nbsp; Note: This appears on each machine/browser from which this site is accessed.
Counting: Base conversions
1. Counting: Base conversions
2. Integer division
Integer division yields a quotient and a remainder.
Integer division of x by divisor b yields a quotient q and a remainder r.
3. Mathematical notation
q is expressed as x div b.
r is expressed as x mod b.
4. Visual Basic
q is expressed as Int(x / b).
r is expressed as x Mod b.
5. Converting between bases
We will now look at a simple method for converting between bases. The method works for numbers in the range 0 to 255.
6. Converting decimal to hex
Divide
x by
16 to get quotient
q and remainder
r. Convert
q to hex and
r to hex.
156 div 16 is 9d is 9h
156 mod 16 is 12d is Ch
So,
156d is
9Ch.
7. Converting hex to decimal
9h is 9d
Ch is 12d
9 * 16 + 12 = 156d
So,
9Ch is
156d
8. Converting decimal to binary
Question: How can decimal be converted to binary?
9. Converting hex to binary
To convert decimal to binary, convert decimal to hex, and then convert hex to binary.
9h is 1001b
Ch is 1100b
So,
156d is
9Ch is
10011100b.
10. Converting binary to hex
1001b is 9h
1100b is Ch
So,
10011100b is
9Ch.
11. Converting binary to decimal
Question: How can binary be converted to decimal?
12. Transitivity
To convert binary to decimal, convert binary to hex, and then convert hex to decimal.
If
we can get from a to b, and
we can get from b to c,
then, by
transitivity,
we can get from a to c.
13. Windows calculator
14. Algorithms
An algorithm is a precise step-by-step method that, if followed, will solve a problem.
A flowchart is a pictorial way of expressing an algorithm.
A pseudo-code is a written way to express an algorithm.
All computers follow algorithms.
15. Computer science
The goal of computer science is to search for finite approximations of potentially infinite objects, usually in the form of programs that contain data structures and algorithms.
Thus, we will spend time studying both data structures and algorithms.
16. Programs
A
program consists of
data structures (data) and algorithms (code). Goal:
Write out the numbers from
0 to
255 (decimal), in order, in decimal, hexadecimal, and binary.
17. Pseudo-code algorithm
get a blank piece of paper
FOR EACH decimal number x from 0 to 255 DO
start a new line
write the number in decimal
write the number in hexadecimal:
divide x by 16
write the quotient in hex
write the remainder in hex
write the number in binary:
convert the first hex digit to binary
convert the second hex digit to binary
END
18. Pascal program (data)
program numbers;
const
digits: array[0..15] of char =
'0123456789ABCDEF';
binary: array[0..15] of string[4] = (
'0000','0001','0010','0011',
'0100','0101','0110','0111',
'1000','1001','1010','1011',
'1100','1101','1110','1111');
var i,h0,h1: word;
19. Pascal program (code)
begin
writeln;
write(' Dec Hex Bin');
for i:= 0 to 255 do begin
writeln;
write(' ',i:4,'d');
h1:= i div 16;
h0:= i mod 16;
write(' ',digits[h1],digits[h0],'h');
write(' ',binary[h1],binary[h0],'b');
end;
end.
20. Pascal program (output)
Dec Hex Bin
0d 00h 00000000b
1d 01h 00000001b
2d 02h 00000010b
3d 03h 00000011b
4d 04h 00000100b
5d 05h 00000101b
6d 06h 00000110b
7d 07h 00000111b
8d 08h 00001000b
9d 09h 00001001b
10d 0Ah 00001010b
11d 0Bh 00001011b
12d 0Ch 00001100b
13d 0Dh 00001101b
14d 0Eh 00001110b
15d 0Fh 00001111b
16d 10h 00010000b
...
21. Pascal program (output)
248d F8h 11111000b
249d F9h 11111001b
250d FAh 11111010b
251d FBh 11111011b
252d FCh 11111100b
253d FDh 11111101b
254d FEh 11111110b
255d FFh 11111111b
22. End of page