BIT: Difference between revisions

From QB64 Phoenix Edition Wiki
Jump to navigation Jump to search
No edit summary
No edit summary
 
(11 intermediate revisions by the same user not shown)
Line 1: Line 1:
{{DISPLAYTITLE:_BIT}}
{{DISPLAYTITLE:_BIT}}
The [[_BIT]] datatype can return only values of 0 (bit off) and -1 (bit on).  
The [[_BIT]] datatype can return only values of 0 (bit off) and -1 (bit on).




{{PageSyntax}}
{{PageSyntax}}
: [[DIM]] {{Parameter|variable}} [[AS]] [{{KW|_UNSIGNED}}] [[_BIT]] [* {{Parameter|numberofbits}}]
: [[DIM]] {{Parameter|variable}} [[AS]] [[[_UNSIGNED]]] [[_BIT]] [* {{Parameter|numberofbits}}]


: [[_DEFINE]] {{Parameter|Letter}}[{{Parameter|-Range}}|,...] [[AS]] [{{KW|_UNSIGNED}}] [[_BIT]] [* {{Parameter|numberofbits}}]
: [[_DEFINE]] {{Parameter|Letter}}[{{Parameter|-Range}}|,...] [[AS]] [[[_UNSIGNED]]] [[_BIT]] [* {{Parameter|numberofbits}}]




Line 15: Line 15:
*The _BIT datatype can be succesfully used as a [[Boolean]] (TRUE or FALSE) and it requires minimal amount of memory (the lowest amount possible actually, one byte can hold 8 bits, if you want to use bits in order to decrease memory usage, use them as arrays as a _BIT variable by itself allocates 4 bytes - DIM bitarray(800) AS _BIT uses 100 bytes).
*The _BIT datatype can be succesfully used as a [[Boolean]] (TRUE or FALSE) and it requires minimal amount of memory (the lowest amount possible actually, one byte can hold 8 bits, if you want to use bits in order to decrease memory usage, use them as arrays as a _BIT variable by itself allocates 4 bytes - DIM bitarray(800) AS _BIT uses 100 bytes).
* '''When a variable has not been assigned or has no type suffix, the value defaults to [[SINGLE]].'''
* '''When a variable has not been assigned or has no type suffix, the value defaults to [[SINGLE]].'''
* '''[[Keywords_currently_not_supported_by_QB64|_BIT is not supported in User Defined TYPES.]]''' Use a [[_BYTE]] and assign up to 8 bit values as shown below.
* '''[[Keywords currently not supported by QB64|_BIT is not supported in User Defined TYPES.]]''' 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.
*'''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: {{InlineCode}}variable` = -1{{InlineCodeEnd}}
:You can define a bit on-the-fly by adding a ` after the variable, like this: {{InlineCode}}variable` = {{Text|-1|#F580B1}}{{InlineCodeEnd}}


:If you want an unsigned bit you can define it on-the-fly by adding ~` instead, like this: {{InlineCode}}variable~` = 1{{InlineCodeEnd}}
:If you want an unsigned bit you can define it on-the-fly by adding ~` instead, like this: {{InlineCode}}variable~` = {{Text|1|#F580B1}}{{InlineCodeEnd}}


:You can set the number of bits on the fly by just adding that number - this defines it as being two bits: {{InlineCode}}variable`2 = -1{{InlineCodeEnd}}
:You can set the number of bits on the fly by just adding that number - this defines it as being two bits: {{InlineCode}}variable`2 = {{Text|-1|#F580B1}}{{InlineCodeEnd}}




Line 32: Line 32:
:* '''"Big-endian"''': MSB is the first bit encountered, decreasing to the LSB as the last bit by position, memory address or time.
:* '''"Big-endian"''': MSB is the first bit encountered, decreasing to the LSB as the last bit by position, memory address or time.
:* '''"Little-endian"''': LSB is the first bit encountered, increasing to the MSB as the last bit by position, memory address or time.
:* '''"Little-endian"''': LSB is the first bit encountered, increasing to the MSB as the last bit by position, memory address or time.
{{WhiteStart}}
{{FixedStart}}
         '''Offset or Position:    0    1  2  3  4  5  6  7      Example: 11110000'''
         '''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
     '''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
  '''Little-Endian Bit On Value:'''    1    2  4  8  16  32  64  128                15
{{WhiteEnd}}
{{FixedEnd}}
::The big-endian method compares exponents of 2<sup>7</sup> down to 2<sup>0</sup> while the little-endian method does the opposite.  
::The big-endian method compares exponents of 2 ^ 7 down to 2 ^ 0 while the little-endian method does the opposite.


<center>'''[[_BYTE|BYTES]]'''</center>
<center>'''[[_BYTE|BYTES]]'''</center>
* [[INTEGER]] values consist of 2 bytes called the '''HI''' and '''LO''' bytes. Anytime that the number of binary digits is a multiple of 16 (2bytes, 4 bytes, etc.) and the HI byte's MSB is on(1), the value returned will be negative. Even with [[SINGLE]] or [[DOUBLE]] values!  
* [[INTEGER]] values consist of 2 bytes called the '''HI''' and '''LO''' bytes. Anytime that the number of binary digits is a multiple of 16 (2bytes, 4 bytes, etc.) and the HI byte's MSB is on(1), the value returned will be negative. Even with [[SINGLE]] or [[DOUBLE]] values!
{{WhiteStart}}                                '''16 BIT INTEGER OR REGISTER'''
{{FixedStart}}                                '''16 BIT INTEGER OR REGISTER'''
               '''AH (High Byte Bits)                        AL (Low Byte Bits)'''
               '''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
   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
   HEX:  8000  4000 2000 1000  800 400  200 100 |  80  40  20  10  8    4  2    1
                                                 |
                                                 <nowiki>|</nowiki>
   DEC: -32768 16384 8192 4096 2048 1024 512 256 | 128  64  32  16  8    4  2    1
   DEC: -32768 16384 8192 4096 2048 1024 512 256 | 128  64  32  16  8    4  2    1
{{WhiteEnd}}
{{FixedEnd}}
::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.
::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.




{{PageExamples}}
{{PageExamples}}
''Example:'' Shifting bits in a value in QB64 versions prior to 1.3 (you can use [[_SHL]] and [[_SHR]] starting with version 1.3).
''Example:'' Shifting bits in a value in QB64 versions prior to 1.3 (you can use [[_SHL]] and [[_SHR]] starting with version 1.3).
{{CodeStart}} '' ''
{{CodeStart}}
n = 24
n = {{Text|24|#F580B1}}
Shift = 3
Shift = {{Text|3|#F580B1}}


{{Cl|PRINT}} LShift(n, Shift)
{{Cl|PRINT}} {{Text|LShift|#55FF55}}(n, Shift)
{{Cl|PRINT}} RShift(n, Shift)
{{Cl|PRINT}} {{Text|RShift|#55FF55}}(n, Shift)
{{Cl|END}}
{{Cl|END}}


{{Cl|FUNCTION}} LShift&(n AS {{Cl|LONG}}, LS AS {{Cl|LONG}})
{{Cl|FUNCTION}} {{Text|LShift&|#55FF55}} (n {{Cl|AS}} {{Cl|LONG}}, LS {{Cl|AS}} {{Cl|LONG}})
IF LS < 0 THEN {{Cl|EXIT FUNCTION}}
    {{Cl|IF}} LS < {{Text|0|#F580B1}} {{Cl|THEN}} {{Cl|EXIT FUNCTION}}
LShift = {{Cl|INT}}(n * (2 ^ LS))  
    {{Text|LShift|#55FF55}} = {{Cl|INT}}(n * ({{Text|2|#F580B1}} ^ LS))
{{Cl|END FUNCTION}}
{{Cl|END FUNCTION}}


{{Cl|FUNCTION}} RShift&(n AS {{Cl|LONG}}, RS AS {{Cl|LONG}})
{{Cl|FUNCTION}} {{Text|RShift&|#55FF55}} (n {{Cl|AS}} {{Cl|LONG}}, RS {{Cl|AS}} {{Cl|LONG}})
IF RS < 0 THEN {{Cl|EXIT FUNCTION}}
    {{Cl|IF}} RS < {{Text|0|#F580B1}} {{Cl|THEN}} {{Cl|EXIT FUNCTION}}
RShift = {{Cl|INT}}(n / (2 ^ RS))
    {{Text|RShift|#55FF55}} = {{Cl|INT}}(n / ({{Text|2|#F580B1}} ^ RS))
{{Cl|END FUNCTION}} '' ''
{{Cl|END FUNCTION}}
{{CodeEnd}}
{{CodeEnd}}
{{small|Adapted from code by RThorpe}}
{{Small|Adapted from code by RThorpe}}
{{OutputStart}} 192
{{OutputStart}} 192
  3
  3
Line 79: Line 79:




{{PageSeeAlso}}  
{{PageSeeAlso}}
* [[&B]] (binary), [[_BYTE]]
* [[&B]] (binary), [[_BYTE]]
* [[_SHL]], [[_SHR]]
* [[_SHL]], [[_SHR]]

Latest revision as of 16:30, 19 March 2023

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

  • An _UNSIGNED _BIT can hold 0 or 1 instead of 0 and -1, if you set the numberofbits you can hold larger values depending on the number of bits you have set (_BIT * 8 can hold the same values as _BYTE for example) and the information below is compromised if setting any number of bits other than 1.
  • If you set the variable to any other number then the least significant bit of that number will be set as the variables number, if the bit is 1 (on) then the variable will be -1 and if the bit is 0 (off) then the variable will be 0.
  • The least significant bit is the last bit on a string of bits (11111) since that bit will only add 1 to the value if set. The most significant bit is the first bit on a string of bits and changes the value more dramatically (significantly) if set on or off.
  • The _BIT datatype can be succesfully used as a Boolean (TRUE or FALSE) and it requires minimal amount of memory (the lowest amount possible actually, one byte can hold 8 bits, if you want to use bits in order to decrease memory usage, use them as arrays as a _BIT variable by itself allocates 4 bytes - DIM bitarray(800) AS _BIT uses 100 bytes).
  • When a variable has not been assigned or has no type suffix, the value defaults to SINGLE.
  • _BIT is not supported in User Defined TYPES. 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


BITS
  • The MSB is the most significant(largest) bit value and LSB is the least significant bit of a binary or register memory address value. The order in which the bits are read determines the binary or decimal byte value. There are two common ways to read a byte:
  • "Big-endian": MSB is the first bit encountered, decreasing to the LSB as the last bit by position, memory address or time.
  • "Little-endian": LSB is the first bit encountered, increasing to the MSB as the last bit by position, memory address or time.
         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 2 ^ 7 down to 2 ^ 0 while the little-endian method does the opposite.
BYTES
  • INTEGER values consist of 2 bytes called the HI and LO bytes. Anytime that the number of binary digits is a multiple of 16 (2bytes, 4 bytes, etc.) and the HI byte's MSB is on(1), the value returned will be negative. Even with SINGLE or DOUBLE values!
                                 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.


Examples

Example: 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
Adapted from code by RThorpe
 192
 3


See also



Navigation:
Main Page with Articles and Tutorials
Keyword Reference - Alphabetical
Keyword Reference - By usage
Report a broken link