&B: Difference between revisions

From QB64 Phoenix Edition Wiki
Jump to navigation Jump to search
m (Protected "&B" ([Edit=Allow only autoconfirmed users] (indefinite) [Move=Allow only autoconfirmed users] (indefinite)))
No edit summary
Tag: Manual revert
 
(3 intermediate revisions by the same user not shown)
Line 1: Line 1:
The '''&B''' prefix denotes that an integer value is expressed in a binary base 2 format using '''QB64''' only.
The [[&B]] prefix denotes that an integer value is expressed in a Binary base 2 format. Every 8 digits represent a [[_BYTE]].




{{PageSyntax}}  
{{PageSyntax}}
:::: a& = '''&B1110110000111111'''
: {{Parameter|a&}} = [[&B]]10010110




* The base 2 numbering system uses binary digit values of 1 or 0, or bits on or bits off in computer register switches or memory.
{{PageDescription}}
* Leading zero values '''can''' be omitted as they add nothing to the byte return value.
* The base 2 numbering system uses binary digit values of 0 and 1 only.
* Eight binary digits would represent a one byte value ranging from 0 to 255. Four digit values("nibbles") range from 0 to 15.
* Leading zero values can be omitted just like in decimal values as they add nothing to the return value.
* Decimal values returned can be any '''signed''' [[INTEGER]], [[LONG]] integer, or [[_INTEGER64]] value so use those type of variables when converting directly as shown in the Syntax. The program [[ERROR Codes|"overflow"]] error limits are listed as:
* Decimal values returned can be any '''signed''' [[INTEGER]], [[LONG]] integer, or [[_INTEGER64]] value so use those type of variables when converting directly as shown above in the Syntax. The program [[ERROR Codes|"overflow"]] error limits are listed as:
** [[INTEGER]]: 16 binary digits or a decimal value range from -32,768 to 32,767
:* [[_BYTE]]: 8 binary digits or a decimal value range from -128 to 127. [[_UNSIGNED]]: 0 to 255.
** [[LONG]]: 32 binary digits or a decimal value range from -2,147,483,648 to 2,147,483,647
:* [[INTEGER]]: 16 binary digits or a decimal value range from -32,768 to 32,767. [[_UNSIGNED]]: 0 to 65535.
** [[_INTEGER64]]: 64 binary digits or decimal values from -9,223,372,036,854,775,808 to 9,223,372,036,854,775,807.
:* [[LONG]]: 32 binary digits or a decimal value range from -2,147,483,648 to 2,147,483,647. [[_UNSIGNED]]: 0 to 4294967295.
* [[LONG]] values can be returned by appending the & or ~%([[_UNSIGNED]] [[INTEGER]]) symbols after the binary number.
:* [[_INTEGER64]]: 64 binary digits or decimal values from -9,223,372,036,854,775,808 to 9,223,372,036,854,775,807.
* [[VAL]] can be used to convert "&B" prefixed string values to decimal.
:* [[_UNSIGNED]] [[_INTEGER64]]: 0 to 18446744073709551615.
* The maximum binary value for each numerical type is the maximum number of digits listed above, each valued at '''1'''.
* Convert binary to [[LONG]] values by appending the values with the suffix '''&'''. Example: [[&B]]1000000000000000 = -32768: [[&B]]1000000000000000'''&''' = 32768
* To convert binary strings returned from [[_BIN$]] with [[VAL]] you need to prefix the string with [[&B]] (for example, if the string is "1101" you should do {{InlineCode}}{{Cl|VAL}}("&B1101"){{InlineCodeEnd}} or {{InlineCode}}{{Cl|VAL}}("&B" + binvalue$){{InlineCodeEnd}}.


<center>'''[[_BIT|BITS]]'''</center>
* 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.
{{PageExamples}}
:* '''"Little-endian"''': LSB is the first bit encountered, increasing to the MSB as the last bit by position, memory address or time.
;Example 1:The maximum binary values of decimal value -1 in each numerical type are:
{{WhiteStart}}
{{CodeStart}}
        '''Offset or Position:    0    1   2  3  4  5  6  7      Example: 11110000'''
c&& = -1: d& = -1: e% = -1: f%% = -1
                              ----------------------------------            --------
bi$ = {{Cl|_BIN$}}(f%%)
    '''Big-Endian Bit On Value:'''  128  64  32  16  8  4  2  1                 240
{{Cl|PRINT}} "Max binary {{Cl|_BYTE}} = "; bi$; " with"; {{Cl|LEN}}(bi$); "digits ="; {{Cl|VAL}}("{{Cl|&B}}" + bi$)
'''Little-Endian Bit On Value:'''    1    2  4  8  16  32  64  128                15
bi$ = {{Cl|_BIN$}}(e%)
{{WhiteEnd}}
{{Cl|PRINT}} "Max binary {{Cl|INTEGER}} = "; bi$; " with"; {{Cl|LEN}}(bi$); "digits ="; {{Cl|VAL}}("{{Cl|&B}}" + bi$)
::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.
bi$ = {{Cl|_BIN$}}(d&)
 
{{Cl|PRINT}} "Max binary {{Cl|LONG}} = "; bi$; " with"; {{Cl|LEN}}(bi$); "digits ="; {{Cl|VAL}}("{{Cl|&B}}" + bi$)
<center>'''[[_BYTE|BYTES]]'''</center>
bi$ = {{Cl|_BIN$}}(c&&)
* [[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!
{{Cl|PRINT}} "Max binary {{Cl|_INTEGER64}} = "; bi$; " with"; {{Cl|LEN}}(bi$); "digits ="; {{Cl|VAL}}("{{Cl|&B}}" + bi$)
{{WhiteStart}}                                 '''16 BIT INTEGER OR REGISTER'''
bi$ = {{Cl|_BIN$}}(9223372036854775807)
              '''AH (High Byte Bits)                        AL (Low Byte Bits)'''
{{Cl|PRINT}} "Max {{Cl|_INTEGER64}} value = "; bi$; " with"; {{Cl|LEN}}(bi$); "digits"
  BIT:    15    14  13  12  11  10  9  8  |   7  6    5  4    3    2  1    0
bi$ = {{Cl|_BIN$}}(-9223372036854775808)
          ---------------------------------------|--------------------------------------
{{Cl|PRINT}} "Min {{Cl|_INTEGER64}} value = "; bi$; " with"; {{Cl|LEN}}(bi$); "digits"
  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
{{WhiteEnd}}
::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. 
 
 
{{TextStart}}                     '''Comparing the Base Numbering Systems'''
 
    '''Decimal (base 10)   Binary (base 2)    Hexadecimal (base 16)    Octal (base 8)'''
 
          0                  0000                  0                    0
          1                  0001                  1                    1
          2                  0010                  2                    2
          3                  0011                  3                    3
          4                  0100                  4                    4
          5                  0101                  5                    5
          6                  0110                  6                    6
          7                  0111                  7                    7 -- maxed
          8                  1000                  8                    10
  maxed-- 9                  1001                  9                    11
        10                  1010                  A                    12
        11                  1011                  B                    13
        12                  1100                  C                    14
        13                  1101                  D                    15
        14                  1110                  E                    16
        15  -------------  1111 <--- Match --->  F  ----------------  17 -- max 2
        16                10000                10                    20
       
      When the Decimal value is 15, the other 2 base systems are all maxed out!
      The Binary values can be compared to all of the HEX value digit values so
      it is possible to convert between the two quite easily. To convert a HEX
      value to Binary just add the 4 binary digits for each HEX digit place so:
 
                        F      A      C      E
              &HFACE = 1111 + 1010 + 1100 + 1101 = &B1111101011001101
 
      To convert a Binary value to HEX you just need to divide the number into
      sections of four digits starting from the right(LSB) end. If one has less
      than 4 digits on the left end you could add the leading zeros like below:
            &B101011100010001001 = 0010 1011 1000 1000 1001 
                      hexadecimal =  2  + B + 8 +  8  + 9 = &H2B889
 
    See the Decimal to Binary conversion function that uses '''[[HEX$]]''' on the '''[[&H]]''' page.
{{TextEnd}}
 
 
''Example 1:'' A Decimal to Binary [[STRING]] function that does not return leading zeroes.
{{CodeStart}} '' ''
{{Cl|PRINT}} BIN$(255)     '1 byte(8 bits) maximum
{{Cl|PRINT}} BIN$(32767)   'integer(2 byte, 15 bits) maximum
{{Cl|PRINT}} BIN$(-32768)   'integer(2 byte, 16 bits) minimum
{{Cl|PRINT}} BIN$(-1)      'all 16 bits on
 
{{Cl|FUNCTION}} BIN$ (n%)
  max% = 8 * {{Cl|LEN}}(n%) ': MSB% = 1  'uncomment for 16 (32 or 64) bit returns
  {{Cl|FOR...NEXT|FOR}} i = max% - 1 {{Cl|TO}} 0 {{Cl|STEP}} -1    'read as big-endian MSB to LSB
    {{Cl|IF...THEN|IF}} (n% {{Cl|AND (boolean)|AND}} 2 ^ i) {{Cl|THEN}} MSB% = 1: B$ = B$ + "1" {{Cl|ELSE}} {{Cl|IF...THEN|IF}} MSB% {{Cl|THEN}} B$ = B$ + "0"
  {{Cl|NEXT}}
{{Cl|IF...THEN|IF}} B$ = "" {{Cl|THEN}} BIN$ = "0" {{Cl|ELSE}} BIN$ = B$   'check for empty string
{{Cl|END FUNCTION}} '' ''
{{CodeEnd}}
{{CodeEnd}}
{{small|Code by Ted Weissgerber}}
{{OutputStart}}
11111111
111111111111111
1000000000000000
1111111111111111
{{OutputEnd}}
''Note:'' The The MSB% flag allows zeroes to be added. Uncomment the MSB% = 1 statement for returns with leading zeroes.
''Example 2:'' QB64 converts the binary values from the example above to [[INTEGER]] decimal values automatically.
{{CodeStart}} '' ''
{{Cl|DEFLNG}} A-Z
a = &B11111111
b = &B111111111111111
c = &B1000000000000000 '& 'or ~%
d = &B1111111111111111 '& 'or ~%
{{Cl|PRINT}} a, b, c, d '' ''
{{CodeEnd}}
{{OutputStart}}
255              32767              -32768              -1
{{OutputEnd}}
:''Bonus example:'' Add an '''&''' symbol after the negative binary numbers to see the [[LONG]] decimal values below.
{{OutputStart}}
{{OutputStart}}
255               32767              32768              65535
Max binary _BYTE = 11111111 with 8 digits = 255
Max binary INTEGER = 1111111111111111 with 16 digits = 65535
Max binary LONG = 11111111111111111111111111111111 with 32 digits = 4294967295
Max binary _INTEGER64 = 1111111111111111111111111111111111111111111111111111111111111111 with 64 digits =-1
Max _INTEGER64 value = 111111111111111111111111111111111111111111111111111111111111111 with 63 digits
Min _INTEGER64 value = 1000000000000000000000000000000000000000000000000000000000000000 with 64 digits
{{OutputEnd}}
{{OutputEnd}}
: ''Note:'' The [[LONG]] values returned are the same as the values you can get using [[_UNSIGNED]] [[INTEGER]] (~%).




{{PageSeeAlso}}
{{PageSeeAlso}}
* [[_BIT]], [[_BYTE]]
* [[_BIN$]], [[HEX$]], [[OCT$]], [[STR$]]
* [[_SHL]], [[_SHR]]
* [[&H]] (hexadecimal), [[&O]] (octal), [[VAL]]
* [[OCT$]], [[&O]] {{text|(octal)}}
* [[Base Comparisons]]
* [[HEX$]], [[&H]] {{text|(hexadecimal)}}




{{PageNavigation}}
{{PageNavigation}}

Latest revision as of 17:17, 25 November 2022

The &B prefix denotes that an integer value is expressed in a Binary base 2 format. Every 8 digits represent a _BYTE.


Syntax

a& = &B10010110


Description

  • The base 2 numbering system uses binary digit values of 0 and 1 only.
  • Leading zero values can be omitted just like in decimal values as they add nothing to the return value.
  • Decimal values returned can be any signed INTEGER, LONG integer, or _INTEGER64 value so use those type of variables when converting directly as shown above in the Syntax. The program "overflow" error limits are listed as:
  • _BYTE: 8 binary digits or a decimal value range from -128 to 127. _UNSIGNED: 0 to 255.
  • INTEGER: 16 binary digits or a decimal value range from -32,768 to 32,767. _UNSIGNED: 0 to 65535.
  • LONG: 32 binary digits or a decimal value range from -2,147,483,648 to 2,147,483,647. _UNSIGNED: 0 to 4294967295.
  • _INTEGER64: 64 binary digits or decimal values from -9,223,372,036,854,775,808 to 9,223,372,036,854,775,807.
  • _UNSIGNED _INTEGER64: 0 to 18446744073709551615.
  • The maximum binary value for each numerical type is the maximum number of digits listed above, each valued at 1.
  • Convert binary to LONG values by appending the values with the suffix &. Example: &B1000000000000000 = -32768: &B1000000000000000& = 32768
  • To convert binary strings returned from _BIN$ with VAL you need to prefix the string with &B (for example, if the string is "1101" you should do VAL("&B1101") or VAL("&B" + binvalue$).


Examples

Example 1
The maximum binary values of decimal value -1 in each numerical type are:
c&& = -1: d& = -1: e% = -1: f%% = -1
bi$ = _BIN$(f%%)
PRINT "Max binary _BYTE = "; bi$; " with"; LEN(bi$); "digits ="; VAL("&B" + bi$)
bi$ = _BIN$(e%)
PRINT "Max binary INTEGER = "; bi$; " with"; LEN(bi$); "digits ="; VAL("&B" + bi$)
bi$ = _BIN$(d&)
PRINT "Max binary LONG = "; bi$; " with"; LEN(bi$); "digits ="; VAL("&B" + bi$)
bi$ = _BIN$(c&&)
PRINT "Max binary _INTEGER64 = "; bi$; " with"; LEN(bi$); "digits ="; VAL("&B" + bi$)
bi$ = _BIN$(9223372036854775807)
PRINT "Max _INTEGER64 value = "; bi$; " with"; LEN(bi$); "digits"
bi$ = _BIN$(-9223372036854775808)
PRINT "Min _INTEGER64 value = "; bi$; " with"; LEN(bi$); "digits"
Max binary _BYTE = 11111111 with 8 digits = 255
Max binary INTEGER = 1111111111111111 with 16 digits = 65535
Max binary LONG = 11111111111111111111111111111111 with 32 digits = 4294967295
Max binary _INTEGER64 = 1111111111111111111111111111111111111111111111111111111111111111 with 64 digits =-1
Max _INTEGER64 value = 111111111111111111111111111111111111111111111111111111111111111 with 63 digits
Min _INTEGER64 value = 1000000000000000000000000000000000000000000000000000000000000000 with 64 digits


See also



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