QB64.com

QB64 is a modern extended BASIC programming language that retains QBasic/QuickBASIC 4.5 compatibility and compiles native binaries for Windows, Linux, and macOS.

The _BIT datatype can return only values of 0 (bit off) and -1 (bit on).

Syntax

DIM variable AS [_UNSIGNED] _BIT [* numberofbits]

_DEFINE Letter[-Range ,…] AS [_UNSIGNED] _BIT [* numberofbits]

Description

Use a _BYTE and assign up to 8 bit values as shown below.

*Suffix Symbols The _BIT type suffix used is below the grave accent (`), usually located under the tilde (~) key (not an apostrophe). Foreign keyboards may not have the ` key. Try Alt+96 in the IDE.

You can define a bit on-the-fly by adding a ` after the variable, like this: variable\ = -1`

If you want an unsigned bit you can define it on-the-fly by adding ~` instead, like this: variable~\ = 1`

You can set the number of bits on the fly by just adding that number - this defines it as being two bits: variable\2 = -1`

_BIT


         **Offset or Position:    0    1   2   3   4   5   6   7      Example: 11110000**
                              ----------------------------------             --------
    **Big-Endian Bit On Value:**   128  64  32  16   8   4   2   1                 240
 **Little-Endian Bit On Value:**    1    2   4   8  16  32  64  128                 15

The big-endian method compares exponents of 27 down to 20 while the little-endian method does the opposite.

_BYTE


                                 **16 BIT INTEGER OR REGISTER**
              **AH (High Byte Bits)                         AL (Low Byte Bits)**
   BIT:    15    14   13   12   11   10   9   8  |   7   6    5   4    3    2   1    0
          ---------------------------------------|--------------------------------------
   HEX:   8000  4000 2000 1000  800 400  200 100 |  80   40  20   10   8    4   2    1
                                                 |
   DEC: -32768 16384 8192 4096 2048 1024 512 256 | 128   64  32   16   8    4   2    1

The HI byte’s MSB is often called the sign bit! When all 16 of the integer binary bits are on, the decimal return is -1.

Example(s)

Shifting bits in a value in QB64 versions prior to 1.3 (you can use _SHL and _SHR starting with version 1.3).


n = 24
Shift = 3

PRINT LShift(n, Shift)
PRINT RShift(n, Shift)
END

FUNCTION LShift&(n AS LONG, LS AS LONG)
IF LS < 0 THEN EXIT FUNCTION
LShift = INT(n * (2 ^ LS))    
END FUNCTION

FUNCTION RShift&(n AS LONG, RS AS LONG)
IF RS < 0 THEN EXIT FUNCTION
RShift = INT(n / (2 ^ RS))
END FUNCTION 


192
3

See Also