## Binary Counter

## written by Teresa Carrigan

- What is it?
- How it works
- How to use it
- Things to notice
- Things to try
- Extending the model
- NetLogo features
- Related models
- Credits and References
- For more information
- Download the model

## 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 thenumber-of-bitsslider to set the number of bits that the counter can hold. Now press thesetupbutton to change the number of bits displayed. This will zero all the bits.The

slow-motionslider 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

incrementbutton adds one to the bit pattern, using rules that work well when the number is positive.The

loopbutton will continuously increment the counter until clicked a second time.The

fourmonitors 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

quizbutton. 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

- Place Value Method
- Counting in Other Number Bases
- Binary, Quaternary, Octal, Hexadecimal
- Horner's Method
## 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:

Contact Teresa Carrigan for appropriate licenses for redistribution for profit.

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

- Null, L. and Lobur, J.
Essentials of Computer Organization and Architecture, First Edition, Jones and Bartlett, pages 38-55.- Dale, N. and Lewis, J.
Computer Science Illuminated, Second Edition, Jones and Bartlett, pages 59-62.- Stallings, W.
Computer Organization and Architecture: Designing for Performance, Sixth Edition, Prentice Hall, pages 286-290.- Tanenbaum, A.
Structured Computer Organization, Fourth Edition, Prentice Hall, pages 631-640.

HomeApplets 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.