Skip to content

_Unsigned _Bit discrepancy #50

@FirbleMan

Description

@FirbleMan

When doing calculations with _Unsigned _Bits, if the number goes negative, QB64 acts inconsistent from how it acts with _Unsigned _Integers.
Whether this is intentional or not, I don't know. However, it appears to me like a bug.

Run the program at the bottom to see what I mean.
Running an _Unsigned _Bit and and _Unsigned _Integer, set to the same value, through the same calculation, yield vastly different results!
Normally QB64 does a sound job of returning a result in the form of the least common denominator variable type. For example, in the calculation, "3 / 2" a "1.5" would be returned. Or another example, "2 - 3" would return -1. Exceptions to this are when the calculation is assigned to a variable. Then the assigned variable's type is used. This is expected behavior.

Now consider the _Unsigned _bit:
In equations resulting in a decimal, nothing unusual happens.
But when things turn negative...
The rules of _Unsigned _Integer variable type seems to be used. That doesn't seem quite right.
For example, "0 - a" where a is dimensioned as _Unsigned _Bit, 4294967295 is returned. Which is -1 for _Unsigned _Integers
Shouldn't, at the very least, _Unsigned _Bit rules of calculation be used, so it would return the top value given the amount of bits dimmensioned?
However, try the same equation with an _Unsigned _Integer, and you get -1!
I believe the way _Unsigned _Integers work is the way _Unsigned _Bits should as well.

Dim UnsignedBIT As _Unsigned _Bit
Dim UnsignedINT As _Unsigned Integer
Dim SignedINT As Integer

UnsignedBIT = 1
UnsignedINT = 1

Print "UnsignedBIT = "; UnsignedBIT;
Print "UnsignedINT = "; UnsignedINT
Print ""
Print "Unsigned bit does not act like unsigned Integer."
Print "-UnsignedBIT = "; -UnsignedBIT
Print "-UnsignedINT = "; -UnsignedINT
Print "0 - UnsignedBIT = "; 0 - UnsignedBIT
Print "0 - UnsignedINT = "; 0 - UnsignedINT
Print ""
Print "Saving to a signed bit works as expected."
SignedINT = -UnsignedBIT
Print "SignedINT = -UnsignedBIT"; SignedINT;
End

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions