This page was automatically generated by NetLogo 2.0.1. Questions, problems? Contact feedback@ccl.northwestern.edu.

The applet requires Java 1.4.1 or higher to run. It will not run on Windows 95 or Mac OS 8 or 9. Mac users must have OS X 10.2.6 or higher and use a browser that supports Java 1.4 applets (Safari works, IE does not). On other operating systems, you may obtain the latest Java plugin from Sun's Java site.


In order for this to work, this file, your model file (BinaryCounter.nlogo), and the file NetLogoLite.jar must all be in the same directory. (You can copy NetLogoLite.jar from the directory where you installed NetLogo.)

On some systems, you can test the applet locally on your computer before uploading it to a web server. It doesn't work on all systems, though, so if it doesn't work from your hard drive, please try uploading it to a web server.

You don't need to include everything in this file in your page. If you want, you can just take the HTML code beginning with <applet> and ending with </applet>, and paste it into any HTML file you want. It's even OK to put multiple <applet> tags on a single page.

If NetLogoLite.jar and your model are in different directories, you must modify the archive= and value= lines in the HTML code to point to their actual locations. (For example, if you have multiple applets in different directories on the same web server, you may want to put a single copy of NetLogoLite.jar in one central place and change the archive= lines of all the HTML files to point to that one central copy. This will save disk space for you and download time for your user.)

created with NetLogo

view/download model file: BinaryCounter.nlogo

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.