# Binary Counter

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

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