Announcement

Collapse
No announcement yet.

BIN$() function

Collapse
X
 
  • Filter
  • Time
  • Show
Clear All
new posts

  • Peter Voll
    replied
    It's terrible when your memory goes
    Memory is the second thing to go, as you get older.
    I can't remember what the first is...

    ------------------

    Leave a comment:


  • Dave Stanton
    replied
    Thanks for the correction.
    It's terrible when your memory goes.
    Should use a better memory manager.


    ------------------
    Dave

    For a real Nightmare

    Leave a comment:


  • Bob Zale
    replied
    Function Bin32(q&)AS STRING
    IF q& >= 0 THEN
    FUNCTION = RIGHT$("00000000000000000000000000000000" + BIN$(q&),32)
    ELSE
    FUNCTION = RIGHT$("11111111111111111111111111111111" + BIN$(q&),32)
    END IF
    END FUNCTION



    ------------------

    Leave a comment:


  • Arthur Gomide
    replied
    Macro? IIF$? PB35?

    ------------------
    Arthur Gomide
    "If you think good architecture is expensive, try bad architecture." -- Brian Foote and Joseph Yoder

    Leave a comment:


  • Bob Zale
    replied
    Precisely the right idea Steve. Very good coding... likely the most efficient possible. But you probably want to make a very small change to:

    macro Bin32(Q ) = RIGHT$(iif$(Q>=0,"00000000000000000000000000000000","11111111111111111111111111111111")+BIN$(Q),32)

    Best regards,

    Bob Zale
    PowerBASIC Inc.


    ------------------

    Leave a comment:


  • Steve Miltiadous
    replied



    How about

    macro Bin32(Q ) = RIGHT$(iif$(Q>0,"00000000000000000000000000000000","11111111111111111111111111111111")+BIN$(Q),32)



    ------------------

    Leave a comment:


  • Heinz Salomon
    replied
    Dave,

    your function wouldn't work for negative arguments between -1 and -32,768:

    Code:
    Bin32$(-1???) = 00000000000000001111111111111111 = 65,535 instead of
                    11111111111111111111111111111111 = -1
    Regards
    Heinz Salomon

    ------------------

    Leave a comment:


  • Dave Stanton
    replied
    What's wrong with:-

    FUNCTION Bin32$(Q???)
    FUNCTION=RIGHT$("00000000000000000000000000000000"+BIN$(Q???),32)
    END FUNCTION



    ------------------
    Dave

    For a real Nightmare

    Leave a comment:


  • Heinz Salomon
    replied
    I understand the point *smile*.

    Code:
    FUNCTION Long2Bin(BYVAL rlngNumber AS LONG) AS STRING
      DIM i AS LOCAL INTEGER
      DIM ptrDigit AS LOCAL BYTE PTR     
      DIM strResult AS STATIC STRING * 32  
      LSET strResult = "00000000000000000000000000000000"
      ptrDigit = VARPTR32(strResult) + 31
      FOR i = 1 TO 32
        IF rlngNumber& MOD 2 THEN
          @ptrDigit = &H31
        END IF
        SHIFT RIGHT rlngNumber&, 1
        DECR ptrDigit
      NEXT
      FUNCTION = strResult
    END FUNCTION
    /EDIT
    still better:

    Code:
    FUNCTION Long2Bin(BYVAL rlngNumber AS LONG) AS STRING
      DIM ptrDigit AS LOCAL BYTE PTR     
      DIM strResult AS STATIC STRING * 32  
      LSET strResult = "00000000000000000000000000000000"
      ptrDigit = VARPTR32(strResult) + 32
      WHILE rlngNumber&
        DECR ptrDigit
        IF rlngNumber& MOD 2 THEN
          @ptrDigit = &H31
        END IF
        SHIFT RIGHT rlngNumber&, 1
      WEND
      FUNCTION = strResult
    END FUNCTION
    Regards
    Heinz Salomon



    [This message has been edited by Heinz Salomon (edited May 04, 2007).]

    Leave a comment:


  • Bob Zale
    replied
    There's nothing at all inconsistent about it. The result is not returned in a fixed length field, but rather the size "floats" based upon the value. This is consistent with the RADIX functions (such as HEX$) found in both PowerBASIC and Microsoft compilers for the past twenty (20) years.


    "If I pass a long integer value to the BIN$() function, I'd expect the result to display the correct 32-bit binary representation of this number, even if the passed value actually falls within the range of a (short) integer."

    The PowerBASIC BIN$() function does precisely that.


    "If - in some cases - I have to expand the results "manually" to match the number of bits of the passed argument, I could as well program my own BIN$() function."

    That's the beauty of a programming language product like PowerBASIC! It presumes that you are a programmer! If you'd like to receive results in a format other than ours, you only need to write your own simple function to do just that! Wouldn't it be more productive to just try that approach?

    Best regards,

    Bob Zale
    PowerBASIC Inc.


    ------------------

    Leave a comment:


  • Heinz Salomon
    replied
    For me, the problem lies in the inconsistency regarding the results of the BIN$() function. If I pass a long integer value to the BIN$() function, I'd expect the result to display the correct 32-bit binary representation of this number, even if the passed value actually falls within the range of a (short) integer. If - in some cases - I have to expand the results "manually" to match the number of bits of the passed argument, I could as well program my own BIN$() function.

    Regards
    Heinz Salomon

    (/edit typo)

    [This message has been edited by Heinz Salomon (edited May 04, 2007).]

    Leave a comment:


  • Bob Zale
    replied
    Unfortunately, both of your inferences are remarkably false. If "All you wanted to know was if BIN$() accepts numbers which fall within the long integer range...", it might have been best to actually try it and then just look at the result?

    Perhaps something like...

    PRINT BIN$(65536)

    Best regards,

    Bob Zale
    PowerBASIC Inc.

    ------------------

    Leave a comment:


  • Heinz Salomon
    replied
    Thanks to all for your replies!

    All I wanted to know was if BIN$() accepts numbers which fall within the long integer range - as described in the online help. Obviously, you only get correct results as long as you pass (short) integers to this function. So if I want to get the binary representation of a number which falls within the long integer range, I have to get by with some workaround (at the expense of "programming comfort" and decreased execution speed).

    Summary:
    Since the BIN$() function doesn't work as described in the documentation, this behaviour is either a bug or the documentation is in error.

    Regards
    Heinz Salomon

    ------------------

    Leave a comment:


  • Robert DeBolt
    replied
    Thanks, Mike and Dave!

    Code:
    printmystuff(0)
    printmystuff(1)
    printmystuff(-1)
    printmystuff(65535)
    printmystuff(65536)
    printmystuff(-65535)
    printmystuff(-65536)
    printmystuff(2147483647)
    printmystuff(-2147483648)
     
      FUNCTION printmystuff(a&)
        print "Number =" + str$(a&)
        print bin$(a&)
        print binfmt$(a&,32)  ' From help screen.
        print bin32$(a&)      ' From Dave Stanton
      END FUNCTION
     
      FUNCTION BinFmt$(value&,size%) ' returns a binary string of size% digits
        if value& < 0 then
          BinFmt$ = RIGHT$("111111111111111"+BIN$(value&),size%)
        else
          BinFmt$ = RIGHT$("000000000000000"+BIN$(value&),size%)
        end if
      END FUNCTION
     
     
      FUNCTION Bin32$(N&)
      FUNCTION = BIN$(N& \ 65536)+ BIN$(n& MOD 65536)
      END FUNCTION
     
     
    Number = 0
    0
    0000000000000000
    00
    Number = 1
    1
    0000000000000001
    01
    Number =-1
    1111111111111111
    1111111111111111111111111111111
    01111111111111111
    Number = 65535
    1111111111111111
    0000000000000001111111111111111
    01111111111111111
    Number = 65536
    10000000000000000
    00000000000000010000000000000000
    10
    Number =-65535
    11111111111111110000000000000001
    11111111111111110000000000000001
    011111111111111110000000000000001
    Number =-65536
    11111111111111110000000000000000
    11111111111111110000000000000000
    11111111111111110
    Number = 2147483647
    1111111111111111111111111111111
    01111111111111111111111111111111
    1111111111111111111111111111111
    Number =-2147483648
    10000000000000000000000000000000
    10000000000000000000000000000000
    10000000000000000

    ------------------
    Regards,
    Bob
    --------
    Composed with EZ-Post!

    Leave a comment:


  • Dave Stanton
    replied
    If I remember correctly from my PBDOS days.

    Deleted

    Edit:- I did NOT remember correctly

    For non-negative integers

    FUNCTION BIN16$(Q??)
    FUNCTION = RIGHT$("0000000000000000"+BIN$(Q??),16)
    END FUNCTION

    FUNCTION Bin32$(Q???)
    FUNCTION = BIN16$(Q??? DIV 65536)+ BIN16$(Q??? MOD 65536)
    END FUNCTION

    ------------------
    Dave

    For a real Nightmare

    [This message has been edited by Dave Stanton (edited May 02, 2007).]

    Leave a comment:


  • Eddy Van Esch
    replied
    Indeed. Adding zeroes does not help .. Read first post ...

    Kind regards


    ------------------
    Eddy
    www.devotechs.com -- HIME Huge Integer Math and Encryption library--

    Leave a comment:


  • Mel Bishop
    replied
    Code:
    te$ = bin$(x)
    do until len(te$) = 32
    te$ = "0" + te$
    loop
    check my code at: http://www.powerbasic.com/support/pb...ad.php?t=24418

    binari$ will return an unsigned binary string up to the capactiy
    of your computer.
    ------------------


    [this message has been edited by mel bishop (edited may 02, 2007).]

    Leave a comment:


  • Eric Pearson
    replied
    > Too bad there is no way to add enough zeroes

    Unless I read the first post incorrectly, that's not a solution.

    -- Eric

    Leave a comment:


  • Michael Mattias
    replied
    >Alas, PB/DOS's BIN$() syntax doesn't allow you to
    >specify the number of digits of the result string

    Well, damn.

    Too bad there is no way to add enough zeroes to the start of whatever BIN$ returns to make it 32 characters long.

    Damn.

    Leave a comment:


  • Heinz Salomon
    replied
    Eddy,

    Alas, PB/DOS's BIN$() syntax doesn't allow you to specify the number of digits of the result string:

    BIN$ function
    ─────────────
    [...]
    Syntax: s$ = BIN$(numeric expression)
    Regards,
    Heinz Salomon

    ------------------

    Leave a comment:

Working...
X