; 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 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. 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 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. & 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 ant true 0 Polygon -7566196 true true 136 61 129 46 144 30 119 45 124 60 114 82 97 37 132 10 93 36 111 84 127 105 172 105 189 84 208 35 171 11 202 35 204 37 186 82 177 60 180 44 159 32 170 44 165 60 Polygon -7566196 true true 150 95 135 103 139 117 125 149 137 180 135 196 150 204 166 195 161 180 174 150 158 116 164 102 Polygon -7566196 true true 149 186 128 197 114 232 134 270 149 282 166 270 185 232 171 195 149 186 149 186 Polygon -7566196 true true 225 66 230 107 159 122 161 127 234 111 236 106 Polygon -7566196 true true 78 58 99 116 139 123 137 128 95 119 Polygon -7566196 true true 48 103 90 147 129 147 130 151 86 151 Polygon -7566196 true true 65 224 92 171 134 160 135 164 95 175 Polygon -7566196 true true 235 222 210 170 163 162 161 166 208 174 Polygon -7566196 true true 249 107 211 147 168 147 168 150 213 150 arrow true 0 Polygon -7566196 true true 150 0 0 150 105 150 105 293 195 293 195 150 300 150 bee true 0 Polygon -256 true false 152 149 77 163 67 195 67 211 74 234 85 252 100 264 116 276 134 286 151 300 167 285 182 278 206 260 220 242 226 218 226 195 222 166 Polygon -16777216 true false 150 149 128 151 114 151 98 145 80 122 80 103 81 83 95 67 117 58 141 54 151 53 177 55 195 66 207 82 211 94 211 116 204 139 189 149 171 152 Polygon -7566196 true true 151 54 119 59 96 60 81 50 78 39 87 25 103 18 115 23 121 13 150 1 180 14 189 23 197 17 210 19 222 30 222 44 212 57 192 58 Polygon -16777216 true false 70 185 74 171 223 172 224 186 Polygon -16777216 true false 67 211 71 226 224 226 225 211 67 211 Polygon -16777216 true false 91 257 106 269 195 269 211 255 Line -1 false 144 100 70 87 Line -1 false 70 87 45 87 Line -1 false 45 86 26 97 Line -1 false 26 96 22 115 Line -1 false 22 115 25 130 Line -1 false 26 131 37 141 Line -1 false 37 141 55 144 Line -1 false 55 143 143 101 Line -1 false 141 100 227 138 Line -1 false 227 138 241 137 Line -1 false 241 137 249 129 Line -1 false 249 129 254 110 Line -1 false 253 108 248 97 Line -1 false 249 95 235 82 Line -1 false 235 82 144 100 bird1 false 0 Polygon -7566196 true true 2 6 2 39 270 298 297 298 299 271 187 160 279 75 276 22 100 67 31 0 bird2 false 0 Polygon -7566196 true true 2 4 33 4 298 270 298 298 272 298 155 184 117 289 61 295 61 105 0 43 boat1 false 0 Polygon -1 true false 63 162 90 207 223 207 290 162 Rectangle -6524078 true false 150 32 157 162 Polygon -16776961 true false 150 34 131 49 145 47 147 48 149 49 Polygon -7566196 true true 158 33 230 157 182 150 169 151 157 156 Polygon -7566196 true true 149 55 88 143 103 139 111 136 117 139 126 145 130 147 139 147 146 146 149 55 boat2 false 0 Polygon -1 true false 63 162 90 207 223 207 290 162 Rectangle -6524078 true false 150 32 157 162 Polygon -16776961 true false 150 34 131 49 145 47 147 48 149 49 Polygon -7566196 true true 157 54 175 79 174 96 185 102 178 112 194 124 196 131 190 139 192 146 211 151 216 154 157 154 Polygon -7566196 true true 150 74 146 91 139 99 143 114 141 123 137 126 131 129 132 139 142 136 126 142 119 147 148 147 boat3 false 0 Polygon -1 true false 63 162 90 207 223 207 290 162 Rectangle -6524078 true false 150 32 157 162 Polygon -16776961 true false 150 34 131 49 145 47 147 48 149 49 Polygon -7566196 true true 158 37 172 45 188 59 202 79 217 109 220 130 218 147 204 156 158 156 161 142 170 123 170 102 169 88 165 62 Polygon -7566196 true true 149 66 142 78 139 96 141 111 146 139 148 147 110 147 113 131 118 106 126 71 box true 0 Polygon -7566196 true true 45 255 255 255 255 45 45 45 butterfly1 true 0 Polygon -16777216 true false 151 76 138 91 138 284 150 296 162 286 162 91 Polygon -7566196 true true 164 106 184 79 205 61 236 48 259 53 279 86 287 119 289 158 278 177 256 182 164 181 Polygon -7566196 true true 136 110 119 82 110 71 85 61 59 48 36 56 17 88 6 115 2 147 15 178 134 178 Polygon -7566196 true true 46 181 28 227 50 255 77 273 112 283 135 274 135 180 Polygon -7566196 true true 165 185 254 184 272 224 255 251 236 267 191 283 164 276 Line -7566196 true 167 47 159 82 Line -7566196 true 136 47 145 81 Circle -7566196 true true 165 45 8 Circle -7566196 true true 134 45 6 Circle -7566196 true true 133 44 7 Circle -7566196 true true 133 43 8 circle false 0 Circle -7566196 true true 35 35 230 person false 0 Circle -7566196 true true 155 20 63 Rectangle -7566196 true true 158 79 217 164 Polygon -7566196 true true 158 81 110 129 131 143 158 109 165 110 Polygon -7566196 true true 216 83 267 123 248 143 215 107 Polygon -7566196 true true 167 163 145 234 183 234 183 163 Polygon -7566196 true true 195 163 195 233 227 233 206 159 sheep false 15 Rectangle -1 true true 90 75 270 225 Circle -1 true true 15 75 150 Rectangle -16777216 true false 81 225 134 286 Rectangle -16777216 true false 180 225 238 285 Circle -16777216 true false 1 88 92 spacecraft true 0 Polygon -7566196 true true 150 0 180 135 255 255 225 240 150 180 75 240 45 255 120 135 thin-arrow true 0 Polygon -7566196 true true 150 0 0 150 120 150 120 293 180 293 180 150 300 150 truck-down false 0 Polygon -7566196 true true 225 30 225 270 120 270 105 210 60 180 45 30 105 60 105 30 Polygon -8716033 true false 195 75 195 120 240 120 240 75 Polygon -8716033 true false 195 225 195 180 240 180 240 225 truck-left false 0 Polygon -7566196 true true 120 135 225 135 225 210 75 210 75 165 105 165 Polygon -8716033 true false 90 210 105 225 120 210 Polygon -8716033 true false 180 210 195 225 210 210 truck-right false 0 Polygon -7566196 true true 180 135 75 135 75 210 225 210 225 165 195 165 Polygon -8716033 true false 210 210 195 225 180 210 Polygon -8716033 true false 120 210 105 225 90 210 turtle true 0 Polygon -7566196 true true 138 75 162 75 165 105 225 105 225 142 195 135 195 187 225 195 225 225 195 217 195 202 105 202 105 217 75 225 75 195 105 187 105 135 75 142 75 105 135 105 wolf false 0 Rectangle -7566196 true true 15 105 105 165 Rectangle -7566196 true true 45 90 105 105 Polygon -7566196 true true 60 90 83 44 104 90 Polygon -16777216 true false 67 90 82 59 97 89 Rectangle -1 true false 48 93 59 105 Rectangle -16777216 true false 51 96 55 101 Rectangle -16777216 true false 0 121 15 135 Rectangle -16777216 true false 15 136 60 151 Polygon -1 true false 15 136 23 149 31 136 Polygon -1 true false 30 151 37 136 43 151 Rectangle -7566196 true true 105 120 263 195 Rectangle -7566196 true true 108 195 259 201 Rectangle -7566196 true true 114 201 252 210 Rectangle -7566196 true true 120 210 243 214 Rectangle -7566196 true true 115 114 255 120 Rectangle -7566196 true true 128 108 248 114 Rectangle -7566196 true true 150 105 225 108 Rectangle -7566196 true true 132 214 155 270 Rectangle -7566196 true true 110 260 132 270 Rectangle -7566196 true true 210 214 232 270 Rectangle -7566196 true true 189 260 210 270 Line -7566196 true 263 127 281 155 Line -7566196 true 281 155 281 192 wolf-left false 3 Polygon -6524078 true true 117 97 91 74 66 74 60 85 36 85 38 92 44 97 62 97 81 117 84 134 92 147 109 152 136 144 174 144 174 103 143 103 134 97 Polygon -6524078 true true 87 80 79 55 76 79 Polygon -6524078 true true 81 75 70 58 73 82 Polygon -6524078 true true 99 131 76 152 76 163 96 182 104 182 109 173 102 167 99 173 87 159 104 140 Polygon -6524078 true true 107 138 107 186 98 190 99 196 112 196 115 190 Polygon -6524078 true true 116 140 114 189 105 137 Rectangle -6524078 true true 109 150 114 192 Rectangle -6524078 true true 111 143 116 191 Polygon -6524078 true true 168 106 184 98 205 98 218 115 218 137 186 164 196 176 195 194 178 195 178 183 188 183 169 164 173 144 Polygon -6524078 true true 207 140 200 163 206 175 207 192 193 189 192 177 198 176 185 150 Polygon -6524078 true true 214 134 203 168 192 148 Polygon -6524078 true true 204 151 203 176 193 148 Polygon -6524078 true true 207 103 221 98 236 101 243 115 243 128 256 142 239 143 233 133 225 115 214 114 wolf-right false 3 Polygon -6524078 true true 170 127 200 93 231 93 237 103 262 103 261 113 253 119 231 119 215 143 213 160 208 173 189 187 169 190 154 190 126 180 106 171 72 171 73 126 122 126 144 123 159 123 Polygon -6524078 true true 201 99 214 69 215 99 Polygon -6524078 true true 207 98 223 71 220 101 Polygon -6524078 true true 184 172 189 234 203 238 203 246 187 247 180 239 171 180 Polygon -6524078 true true 197 174 204 220 218 224 219 234 201 232 195 225 179 179 Polygon -6524078 true true 78 167 95 187 95 208 79 220 92 234 98 235 100 249 81 246 76 241 61 212 65 195 52 170 45 150 44 128 55 121 69 121 81 135 Polygon -6524078 true true 48 143 58 141 Polygon -6524078 true true 46 136 68 137 Polygon -6524078 true true 45 129 35 142 37 159 53 192 47 210 62 238 80 237 Line -16777216 false 74 237 59 213 Line -16777216 false 59 213 59 212 Line -16777216 false 58 211 67 192 Polygon -6524078 true true 38 138 66 149 Polygon -6524078 true true 46 128 33 120 21 118 11 123 3 138 5 160 13 178 9 192 0 199 20 196 25 179 24 161 25 148 45 140 Polygon -6524078 true true 67 122 96 126 63 144 @#$#@#$#@ NetLogo 2.0.1 @#$#@#$#@ @#$#@#$#@ @#$#@#$#@