Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Padded Binary String
#11
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
[Image: Dont-trust-those-negative-numbers.png]
b = b + ...
Reply
#12
(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
[Image: Dont-trust-those-negative-numbers.png]

And we haven't even gotten Real yet! like 3 and 1/3 ;-))
b = b + ...
Reply
#13
Try:

Print "Back to decimal? "; Val("&B" + bn$ +"&&")
Reply
#14
(08-25-2022, 04:17 PM)bplus Wrote: I don't like those negative numbers!

_BIN$ operates equally to HEX$ and OCT$ with negatives or real numbers.

If fact, when I've implemented _BIN$, I just copied the OCT$ stuff and changed that couple lines performing octal base operations into binary operations and plugged that back in as _BIN$, so don't blame me (_BIN$), blame OCT$ instead Big Grin  Big Grin  Big Grin
Reply
#15
(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
[Image: Dont-trust-those-negative-numbers.png]

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. Tongue
Reply
#16
"You have to specify which variable type you're returning a binary value back to. Is it a byte? integer? long? signed or unsigned?"

I did specify, " Print "Back to decimal?" ";-))

Don't worry about this, I know this will be as fruit fall as getting a Circle fill or reversing Basic's backward ways with the Y axis. It is what it is.
b = b + ...
Reply
#17
You got it "back to decimal". You just got it back as an unsigned value rather than a signed one.

It's the exact same as saying: (-2) ^ 2 = 4, but SQR(4) doesn't give me (-2)!! Wink
Reply




Users browsing this thread: 1 Guest(s)