Author Topic: Official Bin$ function as complement to the &B notation  (Read 11094 times)

0 Members and 1 Guest are viewing this topic.

Offline RhoSigma

  • QB64 Developer
  • Forum Resident
  • Posts: 565
Official Bin$ function as complement to the &B notation
« on: October 05, 2021, 09:56:19 am »
As I'am the one who's responsible for the proper implementaion of the &B prefixed binary number strings into QB64 (https://qb64forum.alephc.xyz/index.php?topic=1933.msg111935#msg111935), it should be probably me too, who provides an appropriate counterpart, a function to do the conversion from any given hex/oct/dec number into binary string.

Sure there are already many approaches for a Bin$ function in the Forum, some of my own, some from @bplus and @SMcNeill and probably others too. However, I felt there should be a function, which best mimics the regular behavior and results of the built-in HEX$ and OCT$ functions, rather than focusing on speed or extended flexibility.

So here is my official Bin$ function, meant as complement to the &B notation:
  • it can handle positive and negative numbers
  • returns the binary string without &B prefix, just as HEX$ and OCT$ do
  • the result for positive numbers is just as long as needed, ie. no leading zeros are returned
  • the result length for negative numbers is determined by the size of the input
BTW:
This update does also fix the recursion issue (https://qb64forum.alephc.xyz/index.php?topic=4209) of the former Bin$ fucntion.

An example using this Bin$ function:
Save as: BinExample.bas (or whatever)
Code: QB64: [Select]
  1. _TITLE "Bin$ Example"
  2. '=== Full description for the Bin$() function is available
  3. '=== in the separate HTML document.
  4. '=====================================================================
  5.  
  6. '-- some usage examples
  7. PRINT "some simple numbers..."
  8. num& = 5: PRINT num&; "= "; Bin$(num&)
  9. num& = -4: PRINT num&; "= "; Bin$(num&)
  10. num& = 32000: PRINT num&; "= "; Bin$(num&)
  11.  
  12. PRINT "works also with &B, &H and &O..."
  13. PRINT " &B1101 = "; Bin$(&B1101)
  14. PRINT " &H211 = "; Bin$(&H211)
  15. PRINT " &O377 = "; Bin$(&O377)
  16.  
  17. PRINT "and even with floating points (converts integer part only)..."
  18. num# = 123.456: PRINT num#; "= "; Bin$(num#)
  19. num# = -60000.25: PRINT num#; "= "; Bin$(num#)
  20. num# = 0.5: PRINT num#; "= "; Bin$(num#)
  21.  
  22. '-- done
  23.  
  24.  
  25.  
  26.  
  27. '--- Full description available in separate HTML document.
  28. '---------------------------------------------------------------------
  29. FUNCTION Bin$ (value&&)
  30. '--- option _explicit requirements ---
  31. DIM temp~&&, binStr$, charPos%, highPos%
  32. '--- init ---
  33. temp~&& = value&&
  34. binStr$ = STRING$(64, "0"): charPos% = 64: highPos% = 64
  35. '--- convert ---
  36.     IF (temp~&& AND 1) THEN ASC(binStr$, charPos%) = 49: highPos% = charPos%
  37.     charPos% = charPos% - 1: temp~&& = temp~&& \ 2
  38. LOOP UNTIL temp~&& = 0
  39. '--- adjust negative size ---
  40. IF value&& < 0 THEN
  41.     IF -value&& < &H0080000000~&& THEN highPos% = 33
  42.     IF -value&& < &H0000008000~&& THEN highPos% = 49
  43.     IF -value&& < &H0000000080~&& THEN highPos% = 57
  44. '--- set result ---
  45. Bin$ = MID$(binStr$, highPos%)
  46.  
  47.  

As it is required to preserve the UTF-8 encoding of the HTML Documentation, it is packed into an 7-zip archive file attached below. The archive does also contain the example from the codebox above.
* Bin.7z (Filesize: 3.17 KB, Downloads: 171)
« Last Edit: January 31, 2022, 07:50:01 pm by RhoSigma »
My Projects:   https://qb64forum.alephc.xyz/index.php?topic=809
GuiTools - A graphic UI framework (can do multiple UI forms/windows in one program)
Libraries - ImageProcess, StringBuffers (virt. files), MD5/SHA2-Hash, LZW etc.
Bonus - Blankers, QB64/Notepad++ setup pack

Offline RhoSigma

  • QB64 Developer
  • Forum Resident
  • Posts: 565
Re: Official Bin$ function as complement to the &B notation
« Reply #1 on: October 05, 2021, 10:01:33 am »
To @The Librarian, this code needs to be updated in the "Utilities" board too. Note that the source link must be changed to this thread. If you take a new screenshot or just remove the old one I leave up to you.

Thanks in advance..
Sincerly RhoSigma
My Projects:   https://qb64forum.alephc.xyz/index.php?topic=809
GuiTools - A graphic UI framework (can do multiple UI forms/windows in one program)
Libraries - ImageProcess, StringBuffers (virt. files), MD5/SHA2-Hash, LZW etc.
Bonus - Blankers, QB64/Notepad++ setup pack

Offline Petr

  • Forum Resident
  • Posts: 1720
  • The best code is the DNA of the hops.
Re: Official Bin$ function as complement to the &B notation
« Reply #2 on: October 05, 2021, 10:41:30 am »
Just a question. Why are you cutting off "unnecessary zeros"? It will be a confusing complication for the reverse transfer. If you work with type _BYTE, are you still waiting for 8 bits? Does trimming make any higher sense?

Offline RhoSigma

  • QB64 Developer
  • Forum Resident
  • Posts: 565
Re: Official Bin$ function as complement to the &B notation
« Reply #3 on: October 05, 2021, 11:10:35 am »
Just a question. Why are you cutting off "unnecessary zeros"? It will be a confusing complication for the reverse transfer. If you work with type _BYTE, are you still waiting for 8 bits? Does trimming make any higher sense?

Quote
However, I felt there should be a function, which best mimics the regular behavior and results of the built-in HEX$ and OCT$ functions,

HEX$ and OCT$ do cut of unnecassary leading zeros, so my Bin$ wil do so. And I see no reason why ommiting leading zeros should complicate the reversion using &B, do you have a specific code example to explain your concerns?
My Projects:   https://qb64forum.alephc.xyz/index.php?topic=809
GuiTools - A graphic UI framework (can do multiple UI forms/windows in one program)
Libraries - ImageProcess, StringBuffers (virt. files), MD5/SHA2-Hash, LZW etc.
Bonus - Blankers, QB64/Notepad++ setup pack

Offline Petr

  • Forum Resident
  • Posts: 1720
  • The best code is the DNA of the hops.
Re: Official Bin$ function as complement to the &B notation
« Reply #4 on: October 05, 2021, 11:41:27 am »
Yes, in thread below - I use methods other than those listed here but it represents this case (now I will send an upgrade there, I found an bug) - in case If is read more values and thanks to the trimming I get a string shorter than 8bite * number of bytes read, I'm afraid that then the reverse conversion could return the wrong value (if I don't watch the length of the returned string at every step).

https://www.qb64.org/forum/index.php?topic=4252.msg136398#msg136398

Offline bplus

  • Global Moderator
  • Forum Resident
  • Posts: 8053
  • b = b + ...
Re: Official Bin$ function as complement to the &B notation
« Reply #5 on: October 05, 2021, 11:49:00 am »
To @The Librarian, this code needs to be updated in the "Utilities" board too. Note that the source link must be changed to this thread. If you take a new screenshot or just remove the old one I leave up to you.

Thanks in advance..
Sincerly RhoSigma

Thankyou @RhoSigma  I have alerted @Qwerkey (and again ;-))  and my go ahead confirmation.

Is this screenshot OK for replacement?
 
BIN$ Demo.PNG


Offline bplus

  • Global Moderator
  • Forum Resident
  • Posts: 8053
  • b = b + ...
Re: Official Bin$ function as complement to the &B notation
« Reply #6 on: October 05, 2021, 11:51:23 am »
Ha! I had forgotten you do negative numbers different than I would but I realize this is for &B compatibility.

Offline RhoSigma

  • QB64 Developer
  • Forum Resident
  • Posts: 565
Re: Official Bin$ function as complement to the &B notation
« Reply #7 on: October 05, 2021, 12:19:38 pm »
Ha! I had forgotten you do negative numbers different than I would but I realize this is for &B compatibility.

Yep, is ok, screenshot shows the same values I get here, thanks god I did it right :)

I'm curious, how would you do the negatives?
My Projects:   https://qb64forum.alephc.xyz/index.php?topic=809
GuiTools - A graphic UI framework (can do multiple UI forms/windows in one program)
Libraries - ImageProcess, StringBuffers (virt. files), MD5/SHA2-Hash, LZW etc.
Bonus - Blankers, QB64/Notepad++ setup pack

Offline bplus

  • Global Moderator
  • Forum Resident
  • Posts: 8053
  • b = b + ...
Re: Official Bin$ function as complement to the &B notation
« Reply #8 on: October 05, 2021, 12:31:33 pm »
Quote
thanks god I did it right :)
LOL the god that let's us do it right.

Quote
I'm curious, how would you do the negatives?
With negatives of course :-))
Negative 4 would be: -100

I like how binary inflates the look of the size of a number, reminds me of the joke:
There are 10 types of people in this world. Those that know Binary and those that don't.


Offline jack

  • Seasoned Forum Regular
  • Posts: 408
Re: Official Bin$ function as complement to the &B notation
« Reply #9 on: October 05, 2021, 01:46:18 pm »
a negative integer is probably stored in two's complement
-4 -> 11111111111111111111111111111100
« Last Edit: October 05, 2021, 01:51:29 pm by jack »

Offline SMcNeill

  • QB64 Developer
  • Forum Resident
  • Posts: 3972
    • Steve’s QB64 Archive Forum
Re: Official Bin$ function as complement to the &B notation
« Reply #10 on: October 05, 2021, 01:50:58 pm »
a negative integer is probably stored in tow's complement

Negatives start with all 1s and count down from there.

11111111 = -1
11111110 = -2
11111101 = -3
11111100 = -4

and so on...
https://github.com/SteveMcNeill/Steve64 — A github collection of all things Steve!

Offline Petr

  • Forum Resident
  • Posts: 1720
  • The best code is the DNA of the hops.
Re: Official Bin$ function as complement to the &B notation
« Reply #11 on: October 05, 2021, 02:18:14 pm »
This means that in binary, the _UNSIGNED _BYTE type with a value of 255 will be written in exactly the same way as the _BYTE type with a value of -1. Good to know!

Offline SMcNeill

  • QB64 Developer
  • Forum Resident
  • Posts: 3972
    • Steve’s QB64 Archive Forum
Re: Official Bin$ function as complement to the &B notation
« Reply #12 on: October 05, 2021, 04:26:29 pm »
This means that in binary, the _UNSIGNED _BYTE type with a value of 255 will be written in exactly the same way as the _BYTE type with a value of -1. Good to know!

With one caveat:  Depending on variable type.

-1 as a byte is 11111111.
-1 as an integer is 1111111111111111.
-1 as an long is 11111111111111111111111111111111.
-1 as an integer64 is 1(64 times).

So you really have to know your data type to be certain when you say -1 = 11111111, because that's only true with bytes.

(And, yes, I noticed you specified unsigned byte vs byte, so you're 100% correct.  I just wanted to add the extra info so nobody would wrongly think that -1 signed = 255 unsigned in *ALL* cases.  ;)  )
https://github.com/SteveMcNeill/Steve64 — A github collection of all things Steve!

Offline RhoSigma

  • QB64 Developer
  • Forum Resident
  • Posts: 565
Re: Official Bin$ function as complement to the &B notation
« Reply #13 on: October 13, 2021, 06:04:12 am »
Just made another tweak to the Bin$() function, which boosts up its speed by approx. 40%, get the newest update from the first post above.
My Projects:   https://qb64forum.alephc.xyz/index.php?topic=809
GuiTools - A graphic UI framework (can do multiple UI forms/windows in one program)
Libraries - ImageProcess, StringBuffers (virt. files), MD5/SHA2-Hash, LZW etc.
Bonus - Blankers, QB64/Notepad++ setup pack

Offline Qwerkey

  • Forum Resident
  • Posts: 755
Re: Official Bin$ function as complement to the &B notation
« Reply #14 on: October 13, 2021, 06:26:56 am »
Hey, how come you were able to edit an existing entry?  Has Fellippe relaxed that rule back to normal?