#How to cast u64 to i64?

1 messages · Page 1 of 1 (latest)

light dune
#

I do know about

const x: u32 = @truncate(an_u64_val);
const what_I_want: u64 = @intCast(x);

but that is quite long. Is there a shorter way for that?

#

Slightly smaller I found is @intCast(@as(u32, @truncate(an_u64_val)))

last cloud
#

nope, that's kinda how you have to do it, either create separate variables or use @as

tropic vector
#

you could also do

const x: i64 = @bitCast(u64_val)
#

if you're certain that it will fit in an i64

jaunty lion
#

make a helper function, you can make it a single character if thats what you want.

#
fn a(in: u64) i64 {
  return @intCast(in);
}

Now using only 3 characters you can cast anything you want. Cant get much shorter than that. a(...)

#

80 character column lengths saved, hundreds of hours wasted in key strokes prevented, world peace achieved, or something like that idk.