BYTE: Difference between revisions
Jump to navigation
Jump to search
BITS
BYTES
Navigation:
Main Page with Articles and Tutorials
Keyword Reference - Alphabetical
Keyword Reference - By usage
Report a broken link
m (Protected "BYTE" ([Edit=Allow only autoconfirmed users] (indefinite) [Move=Allow only autoconfirmed users] (indefinite))) |
No edit summary |
||
(14 intermediate revisions by the same user not shown) | |||
Line 22: | Line 22: | ||
:* '''"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. | ||
{{ | {{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 | ||
{{ | {{FixedEnd}} | ||
::The big-endian method compares exponents of 2 | ::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! | ||
{{ | {{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 | ||
Line 40: | Line 40: | ||
| | | | ||
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 | ||
{{ | {{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}} | ||
:How negative assignments affect the _UNSIGNED value returned by a byte (8 bits). | :How negative assignments affect the _UNSIGNED value returned by a byte (8 bits). | ||
{{CodeStart}} | {{CodeStart}} | ||
{{Cl|DIM}} unsig {{Cl|AS}} {{Cl|_UNSIGNED}} {{Cl|_BYTE}} | {{Cl|DIM}} unsig {{Cl|AS}} {{Cl|_UNSIGNED}} {{Cl|_BYTE}} | ||
Line 53: | Line 51: | ||
{{Cl|CLS}} | {{Cl|CLS}} | ||
unsig = 1 | unsig = {{Text|1|#F580B1}} | ||
sig = 1 | sig = {{Text|1|#F580B1}} | ||
{{Cl|PRINT}} " | {{Cl|PRINT}} {{Text|<nowiki>"&B00000001 = unsigned & signed are both"</nowiki>|#FFB100}} + {{Cl|STR$}}(unsig {{Cl|AND}} sig) | ||
unsig = 127 | unsig = {{Text|127|#F580B1}} | ||
sig = 127 | sig = {{Text|127|#F580B1}} | ||
{{Cl|PRINT}} "&B01111111 = unsigned & signed are both" + {{Cl|STR$}}(unsig {{Cl|AND}} sig) | {{Cl|PRINT}} {{Text|<nowiki>"&B01111111 = unsigned & signed are both"</nowiki>|#FFB100}} + {{Cl|STR$}}(unsig {{Cl|AND}} sig) | ||
unsig = 255 | unsig = {{Text|255|#F580B1}} | ||
sig = 255 | sig = {{Text|255|#F580B1}} | ||
{{Cl|PRINT}} "&B11111111 = unsigned is" + {{Cl|STR$}}(unsig) + " but signed is " + {{Cl|STR$}}(sig) | {{Cl|PRINT}} {{Text|<nowiki>"&B11111111 = unsigned is"</nowiki>|#FFB100}} + {{Cl|STR$}}(unsig) + {{Text|<nowiki>" but signed is "</nowiki>|#FFB100}} + {{Cl|STR$}}(sig) | ||
unsig = 254 | unsig = {{Text|254|#F580B1}} | ||
sig = 254 | sig = {{Text|254|#F580B1}} | ||
{{Cl|PRINT}} "&B11111110 = unsigned is" + {{Cl|STR$}}(unsig) + " but signed is " + {{Cl|STR$}}(sig) | {{Cl|PRINT}} {{Text|<nowiki>"&B11111110 = unsigned is"</nowiki>|#FFB100}} + {{Cl|STR$}}(unsig) + {{Text|<nowiki>" but signed is "</nowiki>|#FFB100}} + {{Cl|STR$}}(sig) | ||
unsig = 253 | unsig = {{Text|253|#F580B1}} | ||
sig = 253 | sig = {{Text|253|#F580B1}} | ||
{{Cl|PRINT}} "&B11111101 = unsigned is" + {{Cl|STR$}}(unsig) + " but signed is " + {{Cl|STR$}}(sig) | {{Cl|PRINT}} {{Text|<nowiki>"&B11111101 = unsigned is"</nowiki>|#FFB100}} + {{Cl|STR$}}(unsig) + {{Text|<nowiki>" but signed is "</nowiki>|#FFB100}} + {{Cl|STR$}}(sig) | ||
{{Cl|PRINT}} | {{Cl|PRINT}} | ||
{{Cl|PRINT}} "The signed value needs the MSB bit for the sign." | {{Cl|PRINT}} {{Text|<nowiki>"The signed value needs the MSB bit for the sign."</nowiki>|#FFB100}} | ||
{{Cl|PRINT}} "The most significant bit is furthest to the left." | {{Cl|PRINT}} {{Text|<nowiki>"The most significant bit is furthest to the left."</nowiki>|#FFB100}} | ||
{{CodeEnd}} | {{CodeEnd}} | ||
{{OutputStart}} | {{OutputStart}} |
Latest revision as of 11:25, 20 March 2023
A _BYTE variable can hold signed variable values from -128 to 127 (one byte or 8 _BITs). Unsigned from 0 to 255.
Syntax
Description
- Signed _BYTE values can range from -128 to 127.
- _UNSIGNED _BYTEs can hold values from 0 to 255. _UNSIGNED expands the range of positive values.
- Can be defined in a QB64 _DEFINE statement using a starting letter range of variable names.
- Also can be used in a subroutine parameter AS _BYTE variable definitions.
- Define a byte using the suffix %% after the variable name: variable%% = -54
- Define an unsigned byte by adding the suffix ~%% after the variable name: variable~%% = 54
- When a variable has not been assigned or has no type suffix, the value defaults to SINGLE.
- 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.
- 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
- How negative assignments affect the _UNSIGNED value returned by a byte (8 bits).
DIM unsig AS _UNSIGNED _BYTE DIM sig AS _BYTE CLS unsig = 1 sig = 1 PRINT "&B00000001 = unsigned & signed are both" + STR$(unsig AND sig) unsig = 127 sig = 127 PRINT "&B01111111 = unsigned & signed are both" + STR$(unsig AND sig) unsig = 255 sig = 255 PRINT "&B11111111 = unsigned is" + STR$(unsig) + " but signed is " + STR$(sig) unsig = 254 sig = 254 PRINT "&B11111110 = unsigned is" + STR$(unsig) + " but signed is " + STR$(sig) unsig = 253 sig = 253 PRINT "&B11111101 = unsigned is" + STR$(unsig) + " but signed is " + STR$(sig) PRINT PRINT "The signed value needs the MSB bit for the sign." PRINT "The most significant bit is furthest to the left." |
&B00000001 = unsigned & signed are both 1 &B01111111 = unsigned & signed are both 127 &B11111111 = unsigned is 255 but signed is -1 &B11111110 = unsigned is 254 but signed is -2 &B11111101 = unsigned is 253 but signed is -3 The signed value needs the MSB bit for the sign. The most significant bit is furthest to the left. |
See also
- _BIT, &B
- _DEFINE, DIM
- _UNSIGNED
- _SHL, _SHR
- Mathematical Operations
- Screen Memory
- Variable Types
- Converting Bytes to Bits