Next - how to implement negative values in binary.
Although it would be acceptable to simply add a negative sign to the far left of any binary number to indicate a negative, this is not how information is stored and used in binary systems, so the sign itself must also be transformed into binary. The easiest way to create negative numbers in binary is to use the most significant bit (the leftmost bit) to act as the sign, where 0 indicates a positive value and 1 indicates a negative value, which is just like putting a negative sign ahead of the number.
However as one of the bits is used for the sign, it effects the range of values that can be held. For example a byte is eight bits 0000 0000 and if it is unsigned it can hold values between 0-255 (1111 1111(binary) = 255(decimal)). However, if the far left bit is used for the sign, it means there are only 7 binary digits to hold the value of number being expressed meaning the range is then -127 to +127 where 0111 1111 = + 127 and 1111 1111 = - 127. The key is remembering that the most significant bit is on for negative numbers and that's all there is to it.
Here is a function that implements the sign and magnitude method just described:
vb Code:
Function DecToBin4(ByVal X, ByVal NumBits&) As String ' Sign Method for Negative Numbers
'Witis
If X < 0 Then DecToBin4 = "-" ' to indicate a negative number
X = Abs(CDec(X)) ' change the number into a decimal type to cope with large numbers
Do
DecToBin4 = (Fix(X / 2) * 2 = Fix(X)) + 1 & DecToBin4 ' check the bit
X = Fix(X / 2) ' dividing by 2 is the same as dividing by 10 in decimal i.e. it moves the decimal one place to the left, and then use fix to get rid of any remainder.
Loop While X > 0
DecToBin4 = String(NumBits - Len(DecToBin4), "0") & DecToBin4 ' as the format function fails with large strings
If Right(DecToBin4, 1) = "-" Then DecToBin4 = "1" & Left$(DecToBin4, Len(DecToBin4) - 1) ' do the negative numbers
End Function
This function can transform even large negative numbers into binary e.g.:
Code:
Private Sub Command1_Click()
Dim n
n = String(28, "8")
Debug.Print DecToBin4(n, 96) ' large postive number
Debug.Print DecToBin4("-" & n, 96) ' large negative number
End Sub
In addition there are several alternative methods of implementing negative numbers in binary, the most widespread being two's complement. Two's complement is somewhat counterintuitive and very unreadable, nonetheless, here is how to implement it:
Take a postive binary number and invert all the bits then add 1
Code:
0000 0001 (+1 binary)
1111 1110 step 1 invert the bits
1111 1111 step 2 add 1 (-1 binary in two's complement)
And here is a function which implements two's complement for large negative numbers, which by the way is implemented by the windows calculator, try it yourself - just set the view to programmer.
Code:
Function DecToBin5(ByVal X, ByVal NumBits&) As String ' Two's Complement Method for Negative Numbers
Dim Neg As Boolean: If X < 0 Then Neg = True ' for negative numbers
X = Abs(CDec(X)) ' change the number into a decimal type to cope with large numbers
Do
DecToBin5 = (Fix(X / 2) * 2 = Fix(X)) * IIf(Neg, -1, 1) + IIf(Neg, 0, 1) & DecToBin5 ' checks the bit (and inverts the bit for negative numbers)
X = Fix(X / 2) ' dividing by 2 is the same as dividing by 10 in decimal i.e. it moves the decimal one place to the left, and then use fix to get rid of any remainder.
Loop While X > 0
DecToBin5 = String(NumBits - Len(DecToBin5), IIf(Neg, "1", "0")) & DecToBin5 ' as the format function fails with large strings
If Neg Then DecToBin5 = Left$(DecToBin5, InStrRev(DecToBin5, "0") - 1) & "1" & String(Len(DecToBin5) - InStrRev(DecToBin5, "0"), "0") ' then add one to negative numbers.
End Function
If you can understand the DecToBin4 and DecToBin5 functions, good job, you can now transform negative values into binary.