UInt64 and shifts

I must be missing something simple here. I have an application I'm writing and having trouble with shifts and 64 bit unsigned integers. Here's some playground code:

import UIKit

let a: UInt64 = 0xFFFFFFFF00000000
let b: UInt64 = 0x0000000000000000
print(String(format: "a = 0x%016X", a))
print(String(format: "b = 0x%016X", b))
print(String(format: "a | b = 0x%016X", a | b))

It produces this output:

a = 0x0000000000000000
b = 0x0000000000000000
a | b = 0x0000000000000000

Why is "a" not equal to 0xFFFFFFFF00000000?

I'm using XCode 14.3.1.

I also found issues shifting a byte into the upper 32 bits of a UInt64:

let c: UInt64 = 0x00000000000000FF
let d: UInt64 = 0x0000000000000000
print(String(format: "c = 0x%016X", c))
print(String(format: "d = 0x%016X", d))
print(String(format: "d | (c << 32) = 0x%016X", d | (c << 32)))

This produces this output:

c = 0x00000000000000FF
d = 0x0000000000000000
d | (c << 32) = 0x0000000000000000
Answered by Scott in 759862022

Your math is fine; it’s the format string that isn’t doing what you expect. The character X in your "0x%016X" means to format a 32-bit value, so it’s truncating the top half. For 64 bits use lX (that’s a lowercase L character, as in “long”).

Accepted Answer

Your math is fine; it’s the format string that isn’t doing what you expect. The character X in your "0x%016X" means to format a 32-bit value, so it’s truncating the top half. For 64 bits use lX (that’s a lowercase L character, as in “long”).

UInt64 and shifts
 
 
Q