08-25-2022, 06:18 PM
(08-25-2022, 04:21 PM)bplus Wrote:(08-25-2022, 04:17 PM)bplus Wrote: I don't like those negative numbers!
Code: (Select All)Do
Input "What ever number "; whatevernumber
bn$ = _Bin$(whatevernumber) 'the binary string returned by _BIN$
dw = 8 'desired field width
If Len(bn$) >= dw Then
Print bn$ 'number already in desired width or even longer
Else
Print Right$(String$(dw, "0") + bn$, dw) 'left padded with zeros
End If
Print "Back to decimal? "; Val("&B" + bn$)
Loop Until whatevernumber = 0
And we haven't even gotten Real yet! like 3 and 1/3 ;-))
Makes perfect sense, if you just think about it for a moment. Some questions can have more than a single answer. After all, let me ask: What is the square root of four??
If you say 2, you're correct!
If you say -2, you're also correct!
In this case, that's more-or-less what you're seeing here -- the correct answer, but you're only showing HALF the correct answer!
Code: (Select All)
Dim x As Long
Do
Input "What ever number "; whatevernumber
bn$ = _Bin$(whatevernumber) 'the binary string returned by _BIN$
dw = 8 'desired field width
If Len(bn$) >= dw Then
Print bn$ 'number already in desired width or even longer
Else
Print Right$(String$(dw, "0") + bn$, dw) 'left padded with zeros
End If
x = Val("&B" + bn$)
Print "Back to decimal? "; x, Val("&B" + bn$) '2 options as your binary value might be either a signed
' or unsigned number, based upon the variable type it's returned to.
' Think of how SQR(4) is both +2 and -2. It has two answers.
' In this case, you have to define which type you want your binary to represent
' SIGNED LONG x, or UNSIGNED LONG by default.
Loop Until whatevernumber = 0 - 1
You have to specify which variable type you're returning a binary value back to. Is it a byte? integer? long? signed or unsigned?
Quick, tell me: What's the value of: 11111111??
Well, you got that wrong! It's 11,111,111!! I never mentioned it was binary, now did I?
Of course, it could also be hexadecimal... Which you still got wrong as you didn't think of that possibility either! Or octal...
You have to define what those 1's and 0's correspond back to, otherwise the computer just has to make a guess for you. In the case of VAL, it's guessing FLOAT -- not even an integer type at all LOL! -- and gives you the unsigned value back. If you want a signed value, make certain you assign the result to a signed variable. Else, expect the wrong answer.