Binary Exercises
Remember: you're doing these with your favorite person next to you.
You'll be doing conversions between binary and decimal, so here are Scheme functions that you can copy into DrRacket to do those conversions so that you can check your answers:
; Convert a binary number to decimal
(define (bin2dec bin-num)
(bin2dec-helper bin-num 0 1))
(define (bin2dec-helper bin answer multiplier)
(cond
((= bin 0) answer)
((> (modulo bin 10) 0) (bin2dec-helper (quotient bin 10) (+ answer multiplier) (* 2 multiplier)))
(else (bin2dec-helper (quotient bin 10) answer (* 2 multiplier)))))
; try: (bin2dec 1011): the answer should be 13
; Convert a decimal number to binary
(define (dec2bin dec-num)
(dec2bin-helper dec-num 0 1))
(define (dec2bin-helper dec answer multiplier)
(cond
((= dec 0) answer)
((> (modulo dec 2) 0) (dec2bin-helper (quotient dec 2) (+ answer multiplier) (* 10 multiplier)))
(else (dec2bin-helper (quotient dec 2) answer (* 10 multiplier)))))
; try: (dec2bin 21): the answer should be 10101
Convert to binary:
9
22
149
Convert to decimal:
1001
10111
1011001
Add the following binary numbers. Afterwards, check your arithmetic by converting the numbers into decimal.
101 + 10
101 + 100
1001 + 1101
11001 + 101101
What happens when you multiply a binary number by 2? Try adding 5 + 5 (in binary, of course), and 11 + 11. What's the relationship between a number and its double?
What happens when you divide (quotient) a binary number by 2? Try looking at 18 and 9, or, for that matter, 13 and 6...