Whiley / RFCs

Request for Comment (RFC) proposals for substantial changes to the Whiley language.
3 stars 2 forks source link

Unsigned Bitwise Shift #94

Open DavePearce opened 3 years ago

DavePearce commented 3 years ago

Currently, Whiley does not have an unsigned bitwise shift operator. This is quite interesting! For example, the following is problematic:

public function to_int(byte b) -> int:
    int r = 0
    int base = 1
    while b != 0b:
        if (b & 0b00000001) == 0b00000001:
            r = r + base
        b = b >> 1
        base = base * 2
    // finally, add the sign
    if r >= 128:
        return -(256-r)
    else:
        return r

This goes into an infinite loop when b on entry represents a negative number. The problem is that 0b11111111 >> 1 leaves 0b11111111 !!

This was actually a bug in the standard library that was picked up by QuickCheck. Therefore, we could really do with an operator such as >>> found in Java.