QB64.org Forum
Active Forums => QB64 Discussion => Topic started by: Cobalt on April 09, 2019, 02:42:00 pm
-
so I was playing with examples for the _SHR wiki page and came across this little oddity.
which displays the value -128
which I thought should technically be -0 as the 7th bit(going from 0 to 7) in a signed _byte is the '-'(sign bit) so shouldn't it just display 0(or more correctly -0 if there was such a thing)?
seems like its treating it as both an UNSIGNED and SIGNED type at the same time.
would this technically be a bug?
-
Not a bug. I found this under _BYTE
þ 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.
You have defined a signed variable that has a range of -128 to 127.
If you wanted an unsigned variable you have to end with ~%%
-
Not a bug. I found this under _BYTE
þ 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.
You have defined a signed variable that has a range of -128 to 127.
If you wanted an unsigned variable you have to end with ~%%
All that Jack002 said.
-
Ah, here I always thought the sign bit was just that, the bit that controlled whether or not to display the '-' sign.
-
In a signed byte the high bit is the sign bit. You can't go higher than 127 for a positive number. You put in 128, that converts to -128.
127 in binary is 0111 1111
128 in binary is 1000 0000
0 in a signed byte is 0000 0000
1 in a signed byte is 0000 0001
-1 is 1111 1111
Think of it like a car speedometer, it was turned back 1 from 0. You look at the high bit, see it is negative, take 2's complement of the rest and it gives you the number. Sounds crazy, but that is how computers store signed numbers.