B.6. Negative Binary Numbers: Two’s Complement Notation

The discussion so far in this appendix has focused on positive numbers. In this section, we explain how computers represent negative numbers using two’s complement notation. First we explain how the two’s complement of a binary number is formed, then we show why it represents the negative value of the given binary number.

Consider a machine with 32-bit integers. Suppose

					Dim value As Integer = 13

The 32-bit representation of value is

00000000 00000000 00000000 00001101

To form the negative of value we first form its one’s complement by combining value with &H7FFFFFFF using Visual Basic’s Xor operator, as in:

onesComplement = value Xor
					&H7FFFFFFF
				

Internally, onesComplement is now value ...

Get Visual Basic 2005 for Programmers: Deitel Developer Series, Second Edition now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.