Author Topic: BIN$ math negatives  (Read 2801 times)

0 Members and 1 Guest are viewing this topic.

Offline bplus

  • Global Moderator
  • Forum Resident
  • Posts: 8053
  • b = b + ...
    • View Profile
BIN$ math negatives
« on: March 02, 2020, 01:01:15 pm »
from RhoSigma
Quote
Oh, just see another reply, so here is the answer for that one.

The output looks as intended by me, here an example to show pos./neg. number behavior at the _BYTE barrier:
Code: QB64: [Select]
'  128 =         10000000 (8 unsigned data bits)
'  127 =          1111111 (7 unsigned data bits)
' -127 =         10000001 (  normal sign + 7 data bits)
' -128 = 1111111110000000 (extended sign + 7 data bits)
note the sign extension for -128, so it can be distinguished from +128, it's similar at the INTEGER/LONG barriers.

However, if you have better sugestions it should be easy enough to adapt the function.

Nothing better to suggest but maybe a little demo or two to explain.

I learned allot just from looking at this sequence (see attached):
Code: QB64: [Select]
  1. _TITLE "Quick BIN$ test" 'b+ 2020-03-02  QB64 v1.4 < important for testing &B numbers
  2.  
  3.     INPUT "Enter a number to convert to test BIN$ > ", test&&
  4.     PRINT BIN$(test&&)
  5. LOOP UNTIL test&& = 0
  6.  
  7.  
  8. 'Function:  Convert any given dec/hex/oct number into a binary string.
  9. '           Can handle positive and negative values and works in that
  10. '           similar to the QB64 built-in HEX$ and OCT$ functions.
  11. '
  12. 'Synopsis:  binary$ = BIN$ (value&&)
  13. '
  14. 'Result:    binary$ --> The binary representation string of the given
  15. '                       number without leading zeros for positive values
  16. '                       and either 8/16/32 or 64 chars for negatives,
  17. '                       depending on the input size.
  18. '
  19. 'Inputs:    value&& --> The pos./neg. number to convert, may also be
  20. '                       given as &H or &O prefixed value.
  21. '
  22. 'Notes:     You may also pass in floating point values, as long as its
  23. '           represented value fits into the _INTEGER64 (&&) input, hence
  24. '           approx. -9.223372036854776E+18 to 9.223372036854776E+18.
  25. '           Different from HEX$ and OCT$, BIN$ won't throw an overflow
  26. '           error, if this range is exceeded, but the result is probably
  27. '           wrong in such a case.
  28. '---------------------------------------------------------------------
  29. FUNCTION BIN$ (value&&)
  30.     '--- option _explicit requirements ---
  31.     DIM temp~&&, charPos%, highPos%
  32.     '--- init ---
  33.     temp~&& = value&&
  34.     BIN$ = STRING$(64, "0"): charPos% = 64: highPos% = 64
  35.     '--- convert ---
  36.     DO
  37.         IF (temp~&& AND 1) THEN MID$(BIN$, charPos%, 1) = "1": highPos% = charPos%
  38.         charPos% = charPos% - 1: temp~&& = temp~&& \ 2
  39.     LOOP UNTIL temp~&& = 0
  40.     '--- adjust negative size ---
  41.     IF value&& < 0 THEN
  42.         IF -value&& < &H0080000000~&& THEN highPos% = 33
  43.         IF -value&& < &H0000008000~&& THEN highPos% = 49
  44.         IF -value&& < &H0000000080~&& THEN highPos% = 57
  45.     END IF
  46.     '--- set result ---
  47.     BIN$ = MID$(BIN$, highPos%)
  48.  
  49.  
« Last Edit: March 02, 2020, 01:02:37 pm by bplus »

Offline RhoSigma

  • QB64 Developer
  • Forum Resident
  • Posts: 565
    • View Profile
Re: BIN$ math negatives
« Reply #1 on: March 02, 2020, 03:31:10 pm »
Yes, this small sequence shows very good what's basically the difference between positive and negative numbers in binary integer logic.

It's the binary complement, also known as - NOT -

NOT 0 = -1    (&B00000000 (&H00) --> &B11111111 (&HFF))
NOT 1 = -2    (&B00000001 (&H01) --> &B11111110 (&HFE))
NOT 2 = -3    (&B00000010 (&H02) --> &B11111101 (&HFD))
NOT 3 = -4    (&B00000011 (&H03) --> &B11111100 (&HFC))
.
.
.
NOT n = -(n+1)

And the gap of (1) between pos./neg. is because in negative numbers one bit is "stolen" from our max. type range to hold the sign and it's the reason why all programming languages have a extra function to negate a value whithout causing the gap. In most high level languages, such as QB64, this function is a simple preceeding minus (-), in other languages, assembler for instance, we have a extra NEG instruction for that purpose.

What many beginners often misinterpret/misunderstand is, that NEG and NOT are two complete different operations, although they have a very similar effect internally.
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