Binary Counter

written by Teresa Carrigan



Top

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.

Top

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.

Top

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.

Top

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.

Top

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?

Top

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.

Top

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.

Top

RELATED MODELS

Top

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:

  1. this copyright notice is included.
  2. this model will not be redistributed for profit without permission from Teresa Carrigan.
Contact Teresa 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.

Top

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.


Home

Applets on this website were written by Teresa Carrigan in 2004, for use in computer science courses at Blackburn College, with the exception of the Fireworks applet. The applets made with NetLogo require Java 1.4.1 or higher to run. The applets made with NetBeans require Java 1.4.2 or higher to run. Applets might not run on Windows 95 or Mac OS 8 or 9. You may obtain the latest Java plugin from Sun's Java site.