Author Topic: An oddity when assigning a value to a signed variable.  (Read 2547 times)

0 Members and 1 Guest are viewing this topic.

Offline Cobalt

  • QB64 Developer
  • Forum Resident
  • Posts: 878
  • At 60 I become highly radioactive!
    • View Profile
An oddity when assigning a value to a signed variable.
« 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.

Code: QB64: [Select]
  1. A%% = 128
  2. PRINT A%%
  3.  

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?
Granted after becoming radioactive I only have a half-life!

Offline Jack002

  • Forum Regular
  • Posts: 123
  • Boss, l wanna talk about arrays
    • View Profile
Re: An oddity when assigning a value to a signed variable.
« Reply #1 on: April 09, 2019, 03:16:13 pm »
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 ~%%
QB64 is the best!

FellippeHeitor

  • Guest
Re: An oddity when assigning a value to a signed variable.
« Reply #2 on: April 09, 2019, 03:24:48 pm »
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.

Offline Cobalt

  • QB64 Developer
  • Forum Resident
  • Posts: 878
  • At 60 I become highly radioactive!
    • View Profile
Re: An oddity when assigning a value to a signed variable.
« Reply #3 on: April 09, 2019, 07:51:07 pm »
Ah, here I always thought the sign bit was just that, the bit that controlled whether or not to display the '-' sign.
Granted after becoming radioactive I only have a half-life!

Offline Jack002

  • Forum Regular
  • Posts: 123
  • Boss, l wanna talk about arrays
    • View Profile
Re: An oddity when assigning a value to a signed variable.
« Reply #4 on: April 09, 2019, 10:15:29 pm »
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.

QB64 is the best!