aeubanks
aeubanks

Reputation: 1311

Swift bitwise shift operations giving compile-time error

It seems that bitwise operators don't work with UInt64 variables in Swift.

class Polyomino {
    var squares : UInt64 = 0
    var xLength : UInt8 = 0
    var yLength : UInt8 = 0

    func addSquareAt(x : UInt8, y : UInt8) {
        squares |= (UInt64(1) << (x + y * 8))
    }
}

The line with the bitwise operator gives the error "could not fin overload for "\=" with the supplied arguments". The same thing goes with the following statement:

squares = squares | (UInt64(1) << (x + y * 8))

I'm assuming this is an error on Apple's part (could be wrong) but is there any way to fix this temporarily or should I just wait for Apple for a fix? Or maybe I'm doing something wrong?

Upvotes: 2

Views: 1699

Answers (1)

Nate Cook
Nate Cook

Reputation: 93276

The type safety in Swift means that you can't do operations between different numeric types. You're trying to shift UInt8s against a UInt64, which isn't supported. Try casting the result to UInt64 instead:

func addSquareAt(x : UInt8, y : UInt8) {
    squares |= 1 << UInt64(x + y * 8)
}

Upvotes: 3

Related Questions