Two's Complement Subtraction
written by Crystal Barchet and 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 subtraction of numbers stored in two's complement format.
HOW IT WORKS
The two binary numbers are padded to eight bits. Next we take the two's complement of the number being subtracted, to get its negative. Finally, we add the two numbers. If the carry out of the last column is different from the carry into it, then overflow occurs (assuming the number of digits is fixed).
HOW TO USE IT
The setup button generates two random numbers stored in two's complement format.The slow-motion slider is an easy way to adjust the speed of the display so you can watch the digits change as the red arrow passes. Set it to zero if you want to just see the answer quickly. 0.5 is a good setting for the first few steps.
The step button does whatever step comes next, and then stops so you can take notes.
The go button finishes the entire problem, at a speed set by the slow-motion slider.
The show-again button starts the exact problem from the beginning. You may then click either the step button or the go button to see the same demonstration.
The quiz button generates a random problem and then asks you to determine the answer. Commas and spaces are ignored, so you may use them to prevent copy errors.
THINGS TO NOTICE
When two numbers of the same sign are subtracted, it is impossible to get an overflow error.If you add two numbers that have the same bit in the left-most column, and the result has a different bit in that column, there is always an overflow error. This is a different way to detect overflow.
No borrows are needed, ever!
THINGS TO TRY
Set the slow-motion to about .50 seconds (or slower) and press the step button a few times. Watch the demonstration of each step.Do each step by hand, then press step to check your work.
Press setup until you see a problem that you think will cause an overflow error. Press run to see if it does overflow.
EXTENDING THE MODEL
Modify the model to show fixed point representation; that is, specify a given number of digits to the right of the decimal place.Allow the user to input the two numbers to be subtracted.
Display the decimal equivalent of the two numbers and the difference.
NETLOGO FEATURES
"ask other-digit-here" is used by the arrow to process the digit it is passing over."count" is used to report the number of agents in the given agentset.
"word" is used to put two inputs together and make them a string.
RELATED MODELS
CREDITS AND REFERENCES
This model was written by Crystal Barchet and Teresa 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: Barchet, C. and Carrigan, T. (2004). Two's Complement Subtraction model. Blackburn College, Carlinville, IL.
In other publications, please use: Copyright 2004 by Crystal R. Barchet and Teresa Carrigan. All rights reserved.
FOR MORE INFORMATION
For more information about two's complement subtraction and overflow detection, see one of the following textbooks:
- Null, L. and Lobur, J. Essentials of Computer Organization and Architecture, First Edition, Jones & Bartlett, pages 48-49; 51-53; 58-59.
- Dale, N. and Lewis, J. Computer Science Illuminated, Second Edition, Jones and Bartlett, page 40.
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.