Bit Shifting in Basic

(subethasoftware.com)

62 points | by efren6 4 days ago

4 comments

  • JdeBP 13 hours ago
    The interesting thing is that there were BASICs where one could do this more directly. The early Microsoft BASICs for OEM home computers, as Commodore and Tandy BASICs here, are not the only games in town at 50 years's remove. Picking the 1970s BASICs misses out on what one could actually do a handful of years later, as vendors improved BASIC with these new ideas like 16-bit processors and whatnot. (-:

    TrueBASIC was done by the Darthmouth people themselves in the middle 1980s, and had BIN$() and CONVERT() to convert a number to and from its binary representation as a string of one and zero characters.

    TurboBASIC had binary numbers as first class numeric literals, &Bxxxxxx, and BIN$().

    Sinclair SuperBASIC for the QL had PEEK_W and PEEK_L, so one didn't have to do the tedious byte multiplication. BBC and ARM BASIC had the pling ('!') operator likewise for word instead of byte accesses.

    When it comes to Unix: Thompson's bas(1) from 1972 did not have any of this, and neither did the DEC PDP basic(6) that was left out of Unix 2nd Edition. But again, that was the 1960s and 1970s. All of these years later we have yabasic that has (alas!) C-style rather than the conventional BASIC-style constants for hexadecimal and binary and bin$() and dec() functions for the string to number conversions in binary, in C style.

  • pcwalton 14 hours ago
    You can shift an integer 1 bit to the left by adding it to itself. This should be faster than multiplying by 2 on ancient CPUs.
    • Findecanor 9 hours ago
      BASIC uses floating point (in software) by default. I'm surprised the example didn't use integer variables: Were they not available in this BASIC? Or are they not as fast as I imagine?

      Because of the floating point "tax", I'd think there is some factor where multiplication would still be faster than doing a tree of additions.

      • Dwedit 7 hours ago
        That's correct, integer variables were not available in a floating point BASIC. Needed to get to newer BASICs before you had stuff like DIM A AS INTEGER. ("dim" because it's for setting the dimensions of an array, but then ended up being use for normal variable declaration)
        • Snarwin 7 hours ago
          The VIC-20's BASIC, which the linked article is about, had integer variables. They were denoted with a % symbol at the end, and did not require a DIM statement to declare.

          Source: https://archive.org/details/VIC-20ProgrammersReferenceGuide1...

          • adrian_b 2 hours ago
            The same was true for the Microsoft BASIC for CP/M.

            I assume that it was also true for the Microsoft BASIC for MS-DOS and later, but on IBM PC clones I had no need to use BASIC, as I had compilers for better programming languages.

            EDIT: Looking now in Wikipedia, I see that the VIC-20 BASIC was derived from the Microsoft BASIC, so it has inherited from there the integer variables marked with "%".

            Most BASIC interpreters bundled with the early microcomputers were derived from the Microsoft BASIC, which was the main product of Microsoft at that time. Therefore it is likely that few, if any, BASIC interpreters for microcomputers lacked integer variables.

          • kbelder 2 hours ago
            I don't believe you gained any performance by declaring them as integers. They were converted to floats for all calculations, then converted back when stored. I think the only advantage was reduced storage space.
            • Dwedit 1 hour ago
              In QBASIC, putting DEFINT A-Z at the beginning of your program did indeed speed it up. That's "DEFault variable type is INTeger (16-bit signed) for all variables with names beginning with A-Z".
              • vunderba 15 minutes ago
                I saw your reply was deleted, so I'm responding here just for future interesting info:

                QuickBASIC 4.5 is not QBASIC. QB 4.5 will use a real x87 if it finds one.

                On builds where DOSBox's FPU is only 64-bit (MSVC builds, ARM builds too), you'll get that drift you were describing describing. (X# 50, 49.99999999999)

                I ran into the same thing when I was working on this a few months ago.

                QBASIC.EXE is different. Its math is written as x87 code, but the linker swaps every FPU opcode for an INT 34h down to Microsoft's software emulator which is built into the interpreter.

              • vunderba 1 hour ago
                Yep. QB veterans remember that you get significant performance gains by setting `DEFINT A-Z` so that variables without type suffixes auto-default to integers. Granted you could still do this by properly affixing a '%' but it was easy to forget.

                In QBASIC, untyped variables default to single-precision floating-point values, so arithmetic operations use floating-point math. Assuming you can do everything with integer math, using `DEFINT` is a good way to make sure your calculations run entirely as integer math (very useful for things like basic arithmetic, indexes, loop counters, etc).

                The QBASIC.EXE intrepreter doesn't even take advantage of a x87 floating-point coprocessor (even if the machine has one), making floating-point operations slower. I'm actually working on a pretty accurate QBasic virtual IDE, and ended up doing a pretty deep dive into this to ensure that its performance was on par with machines of the era.

      • rzzzt 8 hours ago
        It's even easier then, you can just increment the exponent portion.
  • bitwize 4 hours ago
    It never occurred to you to POKE a machine-code routine into memory to do this fast?

    You can do this with many home-computer BASICs. Sadly not TI BASIC (TI-99/4A), at least not without Extended BASIC and the PEB, but most machines of the day were not so limited as that one.

  • martinamaldonad 13 hours ago
    [flagged]