QB64.org Forum

Active Forums => Programs => Topic started by: RhoSigma on October 05, 2021, 09:56:19 am

Title: Official Bin$ function as complement to the &B notation
Post by: RhoSigma 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:
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.
Title: Re: Official Bin$ function as complement to the &B notation
Post by: RhoSigma 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
Title: Re: Official Bin$ function as complement to the &B notation
Post by: Petr 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?
Title: Re: Official Bin$ function as complement to the &B notation
Post by: RhoSigma 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?
Title: Re: Official Bin$ function as complement to the &B notation
Post by: Petr 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 (https://www.qb64.org/forum/index.php?topic=4252.msg136398#msg136398)
Title: Re: Official Bin$ function as complement to the &B notation
Post by: bplus 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?
  [ This attachment cannot be displayed inline in 'Print Page' view ]  
Title: Re: Official Bin$ function as complement to the &B notation
Post by: bplus 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.
Title: Re: Official Bin$ function as complement to the &B notation
Post by: RhoSigma 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?
Title: Re: Official Bin$ function as complement to the &B notation
Post by: bplus 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.

Title: Re: Official Bin$ function as complement to the &B notation
Post by: jack on October 05, 2021, 01:46:18 pm
a negative integer is probably stored in two's complement
-4 -> 11111111111111111111111111111100
Title: Re: Official Bin$ function as complement to the &B notation
Post by: SMcNeill 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...
Title: Re: Official Bin$ function as complement to the &B notation
Post by: Petr 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!
Title: Re: Official Bin$ function as complement to the &B notation
Post by: SMcNeill 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.  ;)  )
Title: Re: Official Bin$ function as complement to the &B notation
Post by: RhoSigma 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.
Title: Re: Official Bin$ function as complement to the &B notation
Post by: Qwerkey 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?
Title: Re: Official Bin$ function as complement to the &B notation
Post by: RhoSigma on October 13, 2021, 07:16:52 am
Hey, how come you were able to edit an existing entry?  Has Fellippe relaxed that rule back to normal?

Yes he did, so let's use it carefully without abuse. It's a perfect feature to do code updates like I did here. Also placing additional thoughs as PS, PPS etc. into a post, but I totally understand his annoyance, if it is used to totally turn the point of a post in an actively ongoing discussion or to wipe out the post entirely. And I guess if it happens again, then he will lock the feature again, and then forever, doesn't matter how much we complain and argue.
Title: Re: Official Bin$ function as complement to the &B notation
Post by: luke on October 13, 2021, 08:06:50 am
Can we add this to QB64? It seems desirable to have built in _BIN$ function, but I'm not sure what capability we have for adding things not written in C.
Title: Re: Official Bin$ function as complement to the &B notation
Post by: SMcNeill on October 13, 2021, 08:28:49 am
Can we add this to QB64? It seems desirable to have built in _BIN$ function, but I'm not sure what capability we have for adding things not written in C.

You can always add the translated code into libqb.cpp.  It's not pretty, but it's functional.
Title: Re: Official Bin$ function as complement to the &B notation
Post by: RhoSigma on October 13, 2021, 08:43:05 am
Can we add this to QB64? It seems desirable to have built in _BIN$ function, but I'm not sure what capability we have for adding things not written in C.
You can always add the translated code into libqb.cpp.  It's not pretty, but it's functional.

I was looking for the possibility to build a native _BIN$ already when adding the &B stuff and part of the work is already done, it's a simple derivate of either the func_oct/func_oct_float or func_hex/func_hex_float in libqb.cpp, maybe it's time to finally finish it, just give me a day or two.
Title: Re: Official Bin$ function as complement to the &B notation
Post by: FellippeHeitor on October 13, 2021, 09:15:24 am
I was looking for the possibility to build a native _BIN$ already when adding the &B stuff and part of the work is already done, it's a simple derivate of either the func_oct/func_oct_float or func_hex/func_hex_float in libqb.cpp, maybe it's time to finally finish it, just give me a day or two.

🤩
Title: Re: Official Bin$ function as complement to the &B notation
Post by: RhoSigma on October 13, 2021, 10:11:54 am
While looking into the routines something bothers me, OCT$ and HEX$ both have a minimum output size worth of 16 bits for negative numbers. In my BIN$ I'll go down to 8 bits, if it's sufficent for the negative number.

So how to interpret this 16 bits minimum in regard to QuickBASIC compatiblity:
1.) Was it originally done that way by Galleon, because QuickBASIC did it the same way? -- Then it should stay that way and I should also take it over into BIN$.
2.) Was it simply because QuickBASIC did not have smaller variable types than INTEGER (16bit) anyways? -- Then I could use this opportunity to adapt OCT$/HEX$ to 8 bits minimum too, as QB64 has it's _BYTE variable type now.

I don't have QuickBASIC ready here, if somebody could just check what OCT$(-1) and HEX$(-1) spit out there in.
Title: Re: Official Bin$ function as complement to the &B notation
Post by: FellippeHeitor on October 13, 2021, 10:41:24 am
QuickBASIC 4.5:
  [ This attachment cannot be displayed inline in 'Print Page' view ]  
Title: Re: Official Bin$ function as complement to the &B notation
Post by: RhoSigma on October 13, 2021, 11:29:47 am
Thank you @FellippeHeitor,

so that's indeed 16 bits even for the smallest negative integer, then I'll do it the same way in the coming _BIN$.
Title: Re: Official Bin$ function as complement to the &B notation
Post by: RhoSigma on October 13, 2021, 12:42:41 pm
@Qwerkey

Thanks Richard for updating BIN$ in the Library. As you can see in the previous posts, I move forward now to make _BIN$ a built-in function in QB64. Until that is done, the library function will certainly serve for many people.
Title: Re: Official Bin$ function as complement to the &B notation
Post by: RhoSigma on October 13, 2021, 06:41:50 pm
@FellippeHeitor @luke,

Couldn't resist, finished it right up today.
Implementation of the new _Bin$ function is done, Pull request #198 (https://github.com/QB64Team/qb64/pull/198) is ready for review and merge.

Note that I used the opportunity also to clarify some details in the OCT$ and HEX$ functions too (comments only) and removed a couple of empty lines.

I've tested the new _Bin$ locally here and compared the results with my function from the initial post, everything works as expected and the results do match each other.
Title: Re: Official Bin$ function as complement to the &B notation
Post by: FellippeHeitor on October 14, 2021, 11:43:11 am
Thank you for that, @RhoSigma! Changes merged and already available in the latest dev build. ✨
Keep 'em coming! ❤️
Title: Re: Official Bin$ function as complement to the &B notation
Post by: RhoSigma on October 14, 2021, 12:36:44 pm
Yep, have seen it, thank you Fellippe and Luke.
Title: Re: Official Bin$ function as complement to the &B notation
Post by: Qwerkey on October 14, 2021, 01:32:36 pm
@Qwerkey

Thanks Richard for updating BIN$ in the Library. As you can see in the previous posts, I move forward now to make _BIN$ a built-in function in QB64. Until that is done, the library function will certainly serve for many people.

Will keep BIN$ in the Library until next full release (v2.1?) and then will remove.
@RhoSigma @bplus
Title: Re: Official Bin$ function as complement to the &B notation
Post by: RhoSigma on October 14, 2021, 01:51:53 pm
Will keep BIN$ in the Library until next full release (v2.1?) and then will remove.
@RhoSigma @bplus

@Qwerkey
Would have been my suggestion too, after v2.1 Stable the Library entry and this entire forum thread can go.

@FellippeHeitor
Oh, thanks again, just saw your invitation into the Team and accepted, glad to be part of it now.
So I guess I can commit to the QB64Team\qb64 repository now directly without the need to fork and pull request. It's time to clone/sync the Repo into GitHub Desktop then.
Title: Re: Official Bin$ function as complement to the &B notation
Post by: FellippeHeitor on October 14, 2021, 01:59:31 pm
Oh, thanks again, just saw your invitation into the Team and accepted, glad to be part of it now.
So I guess I can commit to the QB64Team\qb64 repository now directly without the need to fork and pull request. It's time to clone/sync the Repo into GitHub Desktop then.

That's it, exactly. We currently keep development and master tied to continuous integration, so we commit to master only for stable release, and commit to development when there's a new feature ready, since that'll trigger the dev build generation.

I tend to work locally on a "patches" branch, so that I can keep it in sync with the repo without triggering the CI with every push, then I eventually merge it with Development when it's ready for public testing.

Welcome aboard (again) 😉.