; Binary-Quaternary-Octal-Hexadecimal Simulation ; written by Teresa Carrigan, 2004 globals [ accumulate start-x eq-unsign praise digits myDigits base doneyet? number-of-digits current-group group-by step] breeds [ digit arrow gset from-digit] digit-own [ place-value current-value digit-value group pos ] arrow-own [ accumulator ] patches-own [ name ] gset-own [ group-name ] ; runs setup when program is first loaded to startup setup end ; makes the correct number of digits, and the red arrow ; initializes variables to setup locals [ here-x here-y current n] ca set praise [ "You got it!" "Right!" "Correct" "Awesome!" "Perfect!" ] set digits [ "0" "1" "2" "3" "4" "5" "6" "7" "8" "9" "A" "B" "C" "D" "E" "F" ] set base from-base set myDigits [] set n 0 repeat base [ set myDigits lput (item n digits ) myDigits set n (n + 1) ] set start-x ( .5 * screen-size-x) set here-x (start-x - 3) set here-y 5 set current 1 set n 0 set eq-unsign 0 setup-number-of-digits ; make a turtle for each digit ask patch-at (-1 * here-x + 2) here-y [ set plabel-color white set plabel "Original number:" ] repeat number-of-digits [ cct-digit 1 [ setxy here-x here-y set shape "circle" set color white set size 2 set pos n set n n + 1 set label-color black set place-value current set digit-value random base set label (word (item digit-value myDigits) " ") set current-value (digit-value * place-value) ] set here-x (here-x - 2) set current (current * base) ] ; create red arrow cct-arrow 1 [ setxy (screen-size-x * .5 - 1) 1 set heading -90 set color red set shape "arrow" set accumulator int 0 hideturtle ] ; create explanation bar at the bottom ask patches with [ pycor < -4 ] [ set pcolor blue ] ask patch-at 6 -5 [ set plabel "Converting " + add-commas get-number + " base " + from-base + " to base " + to-base + "." set plabel-color white set name 1 ] ask patch-at 6 -6 [ set plabel "" set name 2 ] set step 1 set current-group 1 end to-report get-from-number locals [ result k] set result plabel-of random-one-of patches with [ name = 1 ] set result remove "Converting " result set k position " " result set result substring result 0 k set result clean-input result report result end to show-again locals [ here-x here-y from-number n current] ask patches with [ plabel = "Original number:" ] [ set here-y pycor ] ask turtles [ die ] set from-number get-from-number ask patches with [ pcolor = black and pycor != here-y ] [ set plabel "" ] set number-of-digits length from-number set current 1 set n 0 set eq-unsign 0 set here-x 10.5 repeat number-of-digits [ cct-digit 1 [ setxy here-x here-y set shape "circle" set color white set size 2 set pos n set label-color black set place-value current set label last from-number set digit-value position label digits set current-value (digit-value * place-value) set n n + 1 set from-number but-last from-number ] set here-x (here-x - 2) set current (current * base) ] ; create red arrow cct-arrow 1 [ setxy (screen-size-x * .5 - 1) 1 set heading -90 set color red set shape "arrow" set accumulator int 0 hideturtle ] set step 1 set current-group 1 ask patches with [ name = 2] [ set plabel "" ] end to setup-group-by locals [ base-list ] set base-list [ 2 4 8 16 ] set group-by ( 1 + position to-base base-list) end to setup-number-of-digits locals [ most least ] ifelse from-base = 2 [ set most 11 set least 4 set number-of-digits int (least + random (most - least)) ] [ set number-of-digits int (2 + random 2) ] end ; randomize settings and start over to setup-random locals [ base-list ] set base-list [ 2 4 8 16 ] set from-base random-one-of base-list set base-list remove from-base base-list set to-base random-one-of base-list setup end ; step to the right, processing each digit that is found to one-step locals [ which-step] set which-step "step" + step if ( step < 6) [ run which-step ] end to step5 locals [ max-group n ch] set accumulate "" set n 0 set max-group group-name-of max-one-of gset [ group-name ] while [ n < max-group ] [ set n (n + 1) set accumulate (word label-of random-one-of gset with [ group-name = n] accumulate) ] ifelse length accumulate > 0 [ set ch first accumulate ] [ set ch "" ] while [ch = "0"] [ set accumulate but-first accumulate ifelse length accumulate > 0 [ set ch first accumulate ] [ set ch "" ] ] if accumulate = "" [ set accumulate "0" ] set accumulate add-commas accumulate wait slow-motion * 3 ask patches with [ name = 2] [ set plabel "Answer = " + accumulate + " base " + to-base + "."] set step (step + 1) end to step4 locals [ ch parent-label] ask patches with [ name = 2] [ set plabel "Convert each group to a single digit in base " + to-base + "."] ask gset [ set parent-label label hatch 1 [ set label-color blue + 3 set label remove " " label-of random-one-of other-turtles-here ifelse length label > 0 [ set ch first label ] [ set ch "" ] while [ch = "0"] [ set label but-first label ifelse length label > 0 [ set ch first label] [ set ch "" ] ] set label calc-hex label set heading 180 fd 1 ] set breed from-digit ] ask patch-at (-.5 * screen-size-x + 2) 0 [ set plabel-color blue + 3 set plabel "Base " + to-base + ":" ] set step (step + 1) end to-report calc-bin [ x ] locals [ result n bits ] set result 0 set bits [ "0" "1" ] set x remove " " x set n position x digits ifelse from-base = 4 [ set bits [ "00" "01" "10" "11" ] ] [ ifelse from-base = 8 [ set bits [ "000" "001" "010" "011" "100" "101" "110" "111" ] ] [ if from-base = 16 [ set bits [ "0000" "0001" "0010" "0011" "0100" "0101" "0110" "0111" "1000" "1001" "1010" "1011" "1100" "1101" "1110" "1111" ] ] ] ] set result item n bits report result end to-report calc-dec [ x ] locals [ result n ] set result 0 while [length x > 0] [ set result 2 * result set n read-from-string first x set result result + n set x but-first x ] report result end to-report calc-hex [ x ] locals [ result n] set result calc-dec x if result > 9 [ set result item result digits ] set result (word "" result) report result end to step3 locals [ n last-digit-x horiz vert finish-digit ] set accumulate "" setup-group-by set n 0 set current-group 1 set horiz (xcor-of max-one-of digit [ xcor ] ) + 2 set finish-digit (xcor-of min-one-of digit [ xcor ] ) - 2 set vert ycor-of random-one-of digit ask patches with [ name = 2 ] [ set plabel "Grouping by " + group-by + "s since " + to-base + " is 2^" + group-by + "."] ask arrow [ setxy horiz vert showturtle set heading -90 while [ xcor > finish-digit] [ if n = group-by [ ask patch-at (group-by - 1) -2 [ sprout 1 [ set shape "box" set color black set label accumulate set heading 0 set breed gset ] ask gset-at 0 0 [ set group-name current-group] ] set n 0 set current-group current-group + 1 set accumulate "" ] fd 1 wait slow-motion if any? digit-here [ set n (n + 1) set last-digit-x xcor ask digit-here [ set accumulate word label accumulate ] ] ] if accumulate > "" [ ask patch-at (last-digit-x - xcor ) (-2) [ sprout 1 [ set shape "box" set color black set label accumulate set heading 0 set breed gset ] ask gset-at 0 0 [ set group-name current-group] ] ] hideturtle ] set step (step + 1) ask patch-at (-.5 * screen-size-x + 2) 1 [ set plabel-color white set plabel "Groups:" ] end to step2 locals [ digitList ] set digitList digit ask digitList [ split who ] ask patch-at (-.5 * screen-size-x + 2) 3 [ set plabel-color yellow set plabel "Binary:" ] ask patches with [name = 2] [ set plabel "Now in Binary." ] set step (step + 1) end to split [ me ] locals [ numbits numsteps what horiz vert] set what label-of random-one-of digit with [ who = me ] set horiz xcor-of random-one-of digit with [ who = me ] set vert ycor-of random-one-of digit with [ who = me ] set numbits length what set numsteps 0 repeat numbits [ hatch 1 [ setxy (horiz - numsteps) (vert - 1) set label last what set what but-last what set numsteps (numsteps + 1) set label-color yellow ] ] wait slow-motion ask digit with [ who = me ] [ set breed from-digit ] end to step1 locals [ numbits base-list parent-label] set numbits 1 set base-list [ 2 4 8 16 ] set numbits ( 1 + position from-base base-list) ask patches with [ name = 2] [ set plabel "Convert each digit to " + numbits + " bits since " + base + " is 2^" + numbits + "."] wait slow-motion ask digit [ if base > 2 [set heading -90 fd pos * numbits ] set parent-label label hatch 1 [ set label calc-bin parent-label set label-color blue + 3 set color black set size .5 set heading 180 fd 1 ] set breed from-digit set color black set label-color white ] ask patch-at (-.5 * screen-size-x + 2) 4 [ set plabel-color blue + 3 set plabel "Step 1:" ] set step (step + 1) wait slow-motion end ; show an entire example from the beginning to the end to go locals [ which-step] set which-step "step" + step ifelse step < 6 [ run which-step set which-step "step" + step wait slow-motion * 3 ] [ stop ] end to-report calc locals [ save-slow-motion] set save-slow-motion slow-motion set slow-motion 0 while [ step < 6 ] [ go ] set accumulate clean-input accumulate set slow-motion save-slow-motion report accumulate end ; asks the user to convert a number to ask-other locals [ guess target question ] without-interruption [ set question get-number ] set question add-commas question set guess user-input (word "Convert the base " from-base " number " question " to base " to-base ".") set guess clean-input guess set target calc ifelse close-enough guess target [ user-message random-one-of praise ] [ user-message "I'm sorry, but the correct answer is " + target] end to-report close-enough [ guess target] locals [ result ] while [ first guess = "0" ] [ set guess but-first guess ] while [ first target = "0" ] [ set target but-first target ] set result ( guess = target ) report result end to-report clean-input [guess] locals [ posit k upper lowerList] set guess remove " " guess set guess remove "," guess set lowerList ["a" "b" "c" "d" "e" "f"] foreach lowerList [ while [member? ? guess] [ set posit position ? guess set k position ? lowerList set upper (item (k + 10) digits) set upper word upper "" set guess replace-item posit guess upper ] ] report guess end ; read the digits, storing it as a string to-report get-number locals [ target n num vert horiz temp-list current-label] set target "" set temp-list digit ; for each digit while [ count temp-list > 0 ] [ set horiz xcor-of max-one-of temp-list [ xcor ] set current-label label-of random-one-of digit with [ xcor = horiz ] set target (word current-label target) ; set temp-list remove-item n temp-list set temp-list digit with [ xcor < horiz ] ] set target remove " " target report target end ; add commas every three digits, so the user won't make copy errors to-report add-commas [ number ] locals [ save k ] set save "" set k 0 while [ (length number) > 0 ] [ set save (word last number save ) set number butlast number set k (k + 1) if (k = 3) and (length number > 0) [ set save (word "," save ) set k 0 ] ] set number save report number end ; ask a quiz question to quiz without-interruption [setup-random ] wait .5 ask-other end ; *** NetLogo Model Copyright Notice *** ; ; Copyright 2004 by Teresa W. Carrigan. All rights reserved. ; ; Permission to use, modify or redistribute this model is hereby granted, ; provided that both of the following requirements are followed: ; a) this copyright notice is included. ; b) this model will not be redistributed for profit without permission ; from Teresa W. Carrigan. ; Contact Teresa W. Carrigan for appropriate licenses for redistribution ; for profit. ; ; To refer to this model in academic publications, please use: ; Carrigan, T. (2004). Binary, Quaternary, Octal, Hexadecimal Simulation model. ; Blackburn College, Carlinville IL. ; ; In other publications, please use: ; Copyright 2004 by Teresa W. Carrigan. All rights reserved. ; ; *** End of NetLogo Model Copyright Notice *** @#$#@#$#@ GRAPHICS-WINDOW 3 10 688 366 13 6 25.0 1 18 1 1 1 CC-WINDOW 10 444 391 507 Command Center BUTTON 11 372 94 405 NIL setup NIL 1 T OBSERVER T SLIDER 188 372 361 405 slow-motion slow-motion 0 1 0.3 0.1 1 seconds BUTTON 284 405 361 438 quiz quiz NIL 1 T OBSERVER T BUTTON 11 406 93 439 step one-step NIL 1 T OBSERVER T BUTTON 95 371 184 404 random setup-random NIL 1 T OBSERVER T BUTTON 95 405 184 438 go go T 1 T OBSERVER T CHOICE 364 374 502 419 from-base from-base 2 4 8 16 3 CHOICE 504 373 642 418 to-base to-base 2 4 8 16 1 BUTTON 186 404 284 437 NIL show-again NIL 1 T OBSERVER T @#$#@#$#@ WHAT IS IT? ----------- This model demonstrates conversion between any two of the following number bases: two, four, eight, sixteen. HOW IT WORKS ------------ Any number that is a base which is a power of two can be converted directly to binary, with each digit becoming the same number of bits as the power of two of the base. For example, eight is two cubed, so to convert a number from base eight to base two, each base eight digit is converted directly to three binary bits. The same trick can be used in reverse when starting with base two: group bits from the right into sets of the correct number of bits, then convert each set to a digit in the new base. To convert between bases four, eight, and sixteen, we simply convert to binary first. This same trick works with any pair of bases that are powers of the same number. It does NOT work in converting between decimal and other bases, unless the other base is 100, 1000, etc. HOW TO USE IT ------------- If you want examples from a specific base to a specific base: Use the to-base and from-base sliders to set the bases. Now press the setup button. This will generate a random number in the from-base to be converted to the to-base. Note that it is possible to have leading zeroes. If you do not care which bases are used, click the random button to generate an arbitrary problem. The slow-motion slider is an easy way to adjust the speed of the display. Set it to zero if you want to show the final result as quickly as possible. 0.3 is a good setting for most purposes. The step button does the next step of the conversion process. It then stops so you can take notes. This is useful when you are first learning the method. The go button does all the remaining steps, at a speed determined by the slow-motion slider. This is useful when you do not need to take notes between each step, or do not wish to press the step button five times to get an answer. If you want to pause the demonstration, simply click the go button a second time and it will stop after it finishes the current step. You may then click go a third time to resume. The quiz button will generate a random problem. Spaces and commas in your answer will be removed, so feel free to use them to help you count the digits. Lower case letters will be automatically changed to upper case. The show-again button starts the exact problem from the beginning. You may then click either the step button or the go button to see the same demonstration. THINGS TO NOTICE ---------------- When converting to a base that is larger, the number of digits will be smaller. When converting to a base that is smaller, the number of digits will be larger. THINGS TO TRY ------------- Set slow-motion to 0.3, click random, and then click go. Set the sliders to a problem type you want to drill, then click setup. Attempt one step at a time on paper, and then click the step button to check that you did that step correctly. EXTENDING THE MODEL ------------------- Modify the model to show fixed point representation; that is, specify a given number of digits to the right of the decimal place. Exactly the same tricks are used in conversion. Allow the user to input a starting digit pattern. Allow the user to input a decimal number, and then display the corresponding digit pattern. Display the decimal equivalent of the digit pattern. Modify the model to demonstrate direct conversion between base 4 and base 16. Binary is not needed because sixteen is four squared. Modify the model to demonstrate direct converstion between base 3 and base 9. Since nine is three squared, each base nine digit directly corresponds to two base three digits. NETLOGO FEATURES ---------------- Instead of nested ifelse blocks, taking the log base two of each base was done using: set base-list [ 2 4 8 16 ] set group-by ( 1 + position to-base base-list) setup-random uses similar list tricks to choose a to-base that is different from the from-base. one-step uses the NetLogo run command combined with a global integer variable step to run the next step, without needing nested ifelse blocks. RELATED MODELS -------------- Place Value Method, Horner's Method, Binary Counter, Counting in Other Number Bases CREDITS AND REFERENCES ---------------------- This model was written by Teresa W. Carrigan, 2004. Permission to use, modify or redistribute this model is hereby granted, provided that both of the following requirements are followed: a) this copyright notice is included. b) this model will not be redistributed for profit without permission from Teresa W. Carrigan. Contact Teresa W. Carrigan for appropriate licenses for redistribution for profit. To refer to this model in academic publications, please use: Carrigan, T. (2004). Binary, Quaternary, Octal, Hexadecimal Simulation model. Blackburn College, Carlinville, IL. In other publications, please use: Copyright 2004 by Teresa W. Carrigan. All rights reserved. FOR MORE INFORMATION -------------------- For more information on conversion between bases that are powers of two, see one of the following textbooks: [1] Dale, N. and Lewis, J. "Computer Science Illuminated", Second Edition, Jones and Bartlett, pages 40-41. [2] Null, L. and Lobur, J. "Essentials of Computer Organization and Architecture", First Edition, Jones and Bartlett, page 44. @#$#@#$#@ default true 0 Polygon -7566196 true true 150 5 40 250 150 205 260 250 arrow true 0 Polygon -7566196 true true 150 0 0 150 105 150 105 293 195 293 195 150 300 150 box true 0 Polygon -7566196 true true 45 255 255 255 255 45 45 45 circle false 0 Circle -7566196 true true 35 35 230 @#$#@#$#@ NetLogo 2.0.1 @#$#@#$#@ @#$#@#$#@ @#$#@#$#@