Julia C constant equivalent

Hi,

What is the equivalent in Julia of 0x8000000000000000ull which is sometimes used in C?

Example in C someone could write :

(x >> 63) | 0x8000000000000000ull

Assuming uint64_t x.

Thank you

You need to use Culonglong(0x8000000000000000).

1 Like

I’m not sure anything particular is required for such cases? For example:

julia> x = typemax(UInt64)
0xffffffffffffffff

julia> (x >> 63)  | 0x8000000000000000
0x8000000000000001

In Julia integer literal are automatically given a “big enough” type, at least up to 128-bit. So 0x8000000000000000 is a UInt64. You can request a specific type by writing e.g. UInt128(0x8000000000000000). But you get UInt128 automatically if you write a larger literal integer:

julia> typeof(0x1000000000000000000000000000000)
UInt128
3 Likes

I am confused now :slight_smile: So which one is the correct answer:

Culonglong(0x8000000000000000)
0x8000000000000000
Or
UInt64(0x8000000000000000)

It seems they all work

The shortest correct answer, assuming you want UInt64, is simply 0x8000000000000000.

As @sudete explained, the type of an integer literal 0x.... in Julia is determined by the number of digits, so there is no need for suffixes. For example, 0x80 is a UInt8, 0x8000 is UInt16, and 0x80000000 is a UInt32.

(If you need a C unsigned long long specifically, you might want to cast it to Culonglong explicitly in case Culonglong is different from a UInt64, but I’m not aware of any supported platform where this is the case. Moreover, the values are converted on assignment anyway, e.g. if you have a ccall with a Culonglong argument or a struct with a Culonglong field to mirror a C type then you can just pass/assign a UInt64 and it will get converted if needed.)

3 Likes