Announcement

Collapse

Maintenance

The forum could be offline for 30-60 minutes in the very near future for maintenance (said 3pm Pacific). I was behind on getting this notice. I do apologize.
See more
See less

BIN$() function

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

  • BIN$() function

    Something seems to be wrong with PB/DOS's BIN$() function.

    The online help says,
    "Numeric expression must be in the range -2,147,483,648 to +2,147,483,647".

    But PRINT BIN$(-1&), LEN(BIN$(-1&)) returns the 16-bit(!) value
    1111111111111111 16
    and not the expected 32-bit value
    11111111111111111111111111111111 32
    which is fatal, as
    CLNG(&B00000000000000001111111111111111) evaluates to 65535 and
    CLNG(&B11111111111111111111111111111111) evaluates to -1.

    Is this a bug or did I misunderstand something?

    Thank you for your replies.

    Heinz Salomon

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

  • #2
    Heinz,

    Have you tried to specify the number of digits, like this?
    PRINT BIN$(-1&, 32)

    Kind regards


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

    Comment


    • #3
      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

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

      Comment


      • #4
        >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.

        Michael Mattias
        Tal Systems Inc. (retired)
        Racine WI USA
        [email protected]
        http://www.talsystems.com

        Comment


        • #5
          > Too bad there is no way to add enough zeroes

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

          -- Eric
          "Not my circus, not my monkeys."

          Comment


          • #6
            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).]
            There are no atheists in a fox hole or the morning of a math test.
            If my flag offends you, I'll help you pack.

            Comment


            • #7
              Indeed. Adding zeroes does not help .. Read first post ...

              Kind regards


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

              Comment


              • #8
                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).]
                Dave.

                You're never too old to learn something stupid.

                Comment


                • #9
                  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!
                  Regards,
                  Bob

                  Comment


                  • #10
                    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

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

                    Comment


                    • #11
                      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.

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

                      Comment


                      • #12
                        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).]

                        Comment


                        • #13
                          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.


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

                          Comment


                          • #14
                            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).]

                            Comment


                            • #15
                              What's wrong with:-

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



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

                              For a real Nightmare
                              Dave.

                              You're never too old to learn something stupid.

                              Comment


                              • #16
                                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

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

                                Comment


                                • #17



                                  How about

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



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

                                  Comment


                                  • #18
                                    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.


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

                                    Comment


                                    • #19
                                      Macro? IIF$? PB35?

                                      ------------------
                                      Arthur Gomide
                                      "If you think good architecture is expensive, try bad architecture." -- Brian Foote and Joseph Yoder
                                      "The trouble with quotes on the Internet is that you can never know if they are genuine." - Abraham Lincoln.

                                      Comment


                                      • #20
                                        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



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

                                        Comment

                                        Working...
                                        X