; BinaryCounter ; written by Teresa Carrigan, 2004 globals [ start-x high-bit eq-unsign eq-sign eq-ones eq-twos praise] breeds [ arrow ] arrow-own [ state ] patches-own [ place-value current-value name ] ; runs setup when program is first loaded to startup setup end ; makes the correct number of bits, and the red arrow ; initializes variables to setup locals [ here-x current ] ca set praise [ "You got it!" "Right!" "Correct" "Awesome!" "Perfect!" ] set start-x 7 set here-x 4 ask patch-at (here-x + 1) 0 [set pcolor white] set current 1 set eq-unsign 0 set eq-sign 0 set eq-ones 0 set eq-twos 0 ; for each bit repeat number-of-bits [ ask patch-at here-x 0 [ set pcolor white set plabel "0" set plabel-color black set here-x (here-x - 1) set place-value current ] set high-bit (current - 1) set current (current * 2) ] ; create red arrow cct-arrow 1 [ setxy start-x 0 set heading -90 set color red set shape "arrow" set state "done" ] ; create explanation bar at the bottom ask patches with [ pycor < -3 ] [ set pcolor blue ] ask patch-at 6 -4 [ set plabel "Counting in Binary " set plabel-color white set name 1 ] ask patch-at 6 -5 [ set plabel "" set name 2 ] end ; add one to the bit pattern to increment set state "add" wait slow-motion ifelse plabel = "0" [ set plabel "1" set current-value place-value ask patches with [name = 2 ] [ set plabel "Change 0 to 1 - done!" ] go-back ] [ ifelse plabel = "1" [ set plabel "0" set current-value 0 fd 1 ask patches with [name = 2 ] [ set plabel "Change 1 to 0, keep going." ] increment ] [ ; check for past left-most bit ifelse xcor < 0 [ ask patches with [name = 2 ] [ set plabel "Overflow!!!" ] go-back ] [ fd 1 increment ] ] ] end ; return red arrow to starting position to go-back set state "return" calc rt 90 fd 5 wait slow-motion rt 90 set xcor start-x wait slow-motion rt 90 fd 5 wait slow-motion rt 90 set state "done" end ; update monitors to calc calc-unsigned calc-signed calc-ones calc-twos end ; decimal equivalent if unsigned binary to calc-unsigned set eq-unsign (sum values-from patches [ current-value ]) end ; decimal equivalent if two's complement to calc-twos locals [ allones neg ] set neg (sum values-from patches [ current-value ]) ifelse neg <= high-bit [ set eq-twos neg ] [ set allones (sum values-from patches [ place-value ]) set eq-twos (neg - allones - 1) ] end ; decimal equivalent if one's complement to calc-ones locals [ allones neg ] set neg (sum values-from patches [ current-value ]) ifelse neg <= high-bit [ set eq-ones neg ] [ set allones (sum values-from patches [ place-value ]) set eq-ones (neg - allones) ] end ; decimal equivalent if signed magnitude to calc-signed locals [ fixsign neg ] set neg (sum values-from patches [ current-value ]) ifelse neg <= high-bit [ set eq-sign neg ] [ set fixsign (high-bit + 1) set eq-sign (neg - fixsign) * -1 ] end ; randomizes the bits showing to random-bits ask patches with [ plabel = "0" or plabel = "1" ] [ set plabel random-one-of [ "0" "1" ] ifelse plabel = "0" [ set current-value 0 ] [ set current-value place-value ] ] end ; asks a quiz question involving counting to ask-count locals [ guess target question] without-interruption [ set question get-number ] set question add-commas question set guess user-input (word "What is one more than " question "?") set guess (word " " guess " ") set guess remove " " guess set guess remove "," guess ask turtles [ go-back increment ] without-interruption [ set target get-number ] ifelse guess = target [ user-message random-one-of praise ] [ set target add-commas target user-message "I'm sorry, but the correct answer is " + target] end ; asks a quiz question involving data representation to ask-other [qt] locals [ guess target question ] without-interruption [ set question get-number ] set question add-commas question set guess user-input (word "Convert the " qt " number " question " to decimal.") set guess remove " " guess set guess remove "," guess set guess read-from-string guess without-interruption [ calc ] ifelse member? "unsign" qt [ set target eq-unsign] [ ifelse member? "sign" qt [ set target eq-sign] [ ifelse member? "one" qt [ set target eq-ones] [ set target eq-twos] ] ] ifelse guess = target [ user-message random-one-of praise ] [ user-message "I'm sorry, but the correct answer is " + int target + "."] end ; read the bits on the patches, storing it as a string to-report get-number locals [ target ] set target "" ask turtles [ while [ xcor > 0 or pcolor = white ] [ if plabel = "0" or plabel = "1" [ set target word plabel target ] fd 1 ] go-back ] set target remove " " target report target end ; add commas every three bits, 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 - calls ask-counting or ask-other to quiz locals [qt flag] ask arrow [ set flag state ] ifelse flag = "done" [without-interruption [random-bits ] wait .5 ifelse quiz-topic = "random" [ set qt random-one-of ["counting" "signed binary" "unsigned binary" "one's complement" "two's complement"] ] [ set qt quiz-topic ] ifelse qt = "counting" [ ask-count ] [ ask-other qt ] ] [ user-message "No quiz until arrow stops." ] 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 Counter 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 388 316 7 5 25.0 1 18 1 1 1 CC-WINDOW 393 261 607 322 Command Center BUTTON 416 42 588 75 NIL setup NIL 1 T OBSERVER T BUTTON 417 75 503 108 NIL increment NIL 1 T TURTLE T SLIDER 417 144 589 177 number-of-bits number-of-bits 2 10 5 1 1 bits BUTTON 503 75 587 108 loop increment T 1 T TURTLE T SLIDER 417 109 590 142 slow-motion slow-motion 0 1 0.25 0.05 1 seconds MONITOR 4 318 111 367 if unsigned binary eq-unsign 0 1 MONITOR 112 318 213 367 if signed binary eq-sign 0 1 MONITOR 214 318 337 367 if one's complement eq-ones 0 1 MONITOR 340 318 464 367 if two's complement eq-twos 0 1 BUTTON 392 179 617 212 QUIZ quiz NIL 1 T OBSERVER T CHOICE 392 214 617 259 quiz-topic quiz-topic "counting" "unsigned binary" "signed binary" "one's complement" "two's complement" "random" 5 @#$#@#$#@ WHAT IS IT? ----------- This model demonstrates counting in binary, and gives the decimal equivalent of the bit pattern for various data representations. This demonstration will help you understand integer overflow and the problems it can cause when undetected. It can also ask you questions, to see how well you understand the concepts. HOW IT WORKS ------------ Each time the binary number is incremented, the red arrow passes to the left. When it finds a 1, it changes the 1 to a 0 but continues to the left. When it finds a 0, it changes the 0 to a 1 and returns to the start position. Each patch holding either a 0 or a 1 has two variables. The place-value is the decimal amount that a 1 in that position would have. The right-most place value is 1, the next is 2, the third is 4, etc. with each place-value doubling the one on its immediate right. The current-value of a patch will equal the place-value if the patch shows a 1, and equal zero otherwise. If a bit pattern is stored using unsigned binary, then the decimal equivalent is the sum of all the current-values of the patches. For the other three representations, if the left-most bit is zero then the decimal equivalent is the same as if it were unsigned binary. When the left-most bit is one, then the decimal equivalent will be negative, and each representation gives a different result. Assuming the left-most bit is one, the signed magnitude equivalent is calculated by adding the current-values of all the patches except the left-most one, and then making that sum negative. If the left-most bit is one, then the one's complement equivalent is calculated by adding the current-values of all the patches (including the left-most one), and then subtracting the sum of all the place-values of the patches. The two's complement equivalent is always one less (more negative) than the one's complement, when the left-most bit is a one. HOW TO USE IT ------------- Use the number-of-bits slider to set the number of bits that the counter can hold. Now press the setup button to change the number of bits displayed. This will zero all the bits. The slow-motion slider is an easy way to adjust the speed of the display so you can watch the bits change as the red arrow moves. Set it to zero if you want to quickly count to a large number. The increment button adds one to the bit pattern, using rules that work well when the number is positive. The loop button will continuously increment the counter until clicked a second time. The four monitors display the decimal equivalent of the bit pattern, assuming that the bits have been stored using each of the four different data representations. To take a quiz, first select the quiz topic, and then click the quiz button. You may choose to include commas or spaces in your answer to help you count digits; they will be ignored. If you choose "random" for your quiz topic, then one of the other possible quiz topics will be chosen for you. THINGS TO NOTICE ---------------- Watch the decimal equivalent monitors while you count. Notice that when the left-most bit is zero, they all agree, but when the left-most bit is one, they all disagree. Is that always the case? Can you find a case where the signed magnitude agrees with either one's complement or two's complement, but not the others? Determine algebraic expressions between the different equivalents. One's complement and two's complement are easy. What is the range of possible values for the different equivalents? Determine algebraic expressions for the ranges, in terms of the number of bits. THINGS TO TRY ------------- Set the slow-motion to about .10 seconds (or slower) and press the increment button a few times. Watch the red arrow change bits, and notice when it decides no more bits need to be changed. What happens when all the bits are one, and you increment again? What happens to the decimal equivalents when all the bits except the left-most one are one, and you increment again? EXTENDING THE MODEL ------------------- Modify the model to show fixed point binary representation; that is, specify a given number of bits to the right of the decimal place. Allow the user to input a starting bit pattern. Allow the user to input a decimal number, and then display the corresponding bit patterns for each of the four data representations. NETLOGO FEATURES ---------------- "sum values-from patches" is used to easily add all the current value of each patch. The startup procedure is used to automatically call setup when the program is opened. "to-report" is used to read the binary number off the patches, and to add commas to a string of bits. RELATED MODELS -------------- Counting in Other Number Bases; Place Value Method; Horner's Method; Binary, Quaternary, Octal, Hexadecimal 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 Counter 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 data representation such as two's complement, see one of these textbooks: [1] Null, L. and Lobur, J. "Essentials of Computer Organization and Architecture", First Edition, Jones and Bartlett, pages 38-55. [2] Dale, N. and Lewis, J. "Computer Science Illuminated", Second Edition, Jones and Bartlett, pages 59-62. [3] Stallings, W. "Computer Organization and Architecture: Designing for Performance", Sixth Edition, Prentice Hall, pages 286-290. [4] Tanenbaum, A. "Structured Computer Organization", Fourth Edition, Prentice Hall, pages 631-640. @#$#@#$#@ 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 thin-arrow true 0 Polygon -7566196 true true 150 0 0 150 120 150 120 293 180 293 180 150 300 150 @#$#@#$#@ NetLogo 2.0.1 @#$#@#$#@ @#$#@#$#@ @#$#@#$#@