Project 6, 2015 Research Experience for Teachers Project 4, 2016
Arithmetic Operations, Integer

Secret Key, Public Key, Hash Algorithms, Protocols, Authentication, Integrity, Confidentiality, Availability

 

Instructions:
    Enter an integer in the upper left textfield labeled "number 1:" and an integer in the textfield below that is labeled "number 2:". Then select an operation (+,-,/,*) from the bottom button group. The binary representation of the two integers shows up in the four textfields to the right of each number. The rightmost byte of each group is the least significant. Integers are typically represented in a computer as a sequence of four bytes. The result of the selected operation shows up in the bottom left textfield labeled "result:". The binary value of the result is shown in the four bytes to its right. Try assigning number 1 the value -3, assigning number 2 the value 1 and adding. Try subtracting the number 3 from the number 1. Any surprises on the binary side?

Twos complement arithmetic:
    It should be easy to see that increasing positive numbers are represented in the sequence 0 (0), 1 (1), 10 (2), 11, (3), 100 (4), 101 (5), 110 (6), 111 (7), 100 (8) and so on. But the negative numbers, in sequence, are ...1111 (-1), ...1110 (-2), ...1101 (-3), ...1100 (-4), ...1011 (-5), ...1010 (-6), ...1001 (-7), ...1000 (-8) and so on. Here is the reason, by example: subtract 6 from 8. That's the same as adding 8 and -6. In binary, we add ...001000 to ...111010. This gives 1000...00010 where the leftmost 1 is outside the group of 4 bytes representing the result. So, we throw it out and what is left is the binary representation of the number 2. Try subtracting 8 from 6 or pick your own experiment. The particular representation of negative numbers (and thereby sacrifice of half of the representable positive numbers) is called twos complement representation and is used to make addition and subtraction easy by removing special cases.