79626663

Date: 2025-05-17 14:51:50
Score: 1
Natty:
Report link

If you have a positive number on a signed int.

const i: i32 = 123;

~i will be negative.

assert(~i < 0)

this could be solved with a @bitCast() since @intCast() does not allow to change the sign.

const u: u32 = @bitCast(~i)

2nd problem: c_ulong and c_int have different bitsized

@bitCast only works if they have the same size.

Solution 1: First use @intCast() and then use ~ that you don't cast a negative number.

Solution 2: Use a @bitCast() to change the type from c_int to c_uint. and then use @intCast() from c_uint to c_ulong.

Reasons:
  • Long answer (-0.5):
  • Has code block (-0.5):
  • User mentioned (1): @bitCast
  • User mentioned (0): @intCast
  • User mentioned (0): @bitCast
  • User mentioned (0): @intCast
  • User mentioned (0): @bitCast
  • User mentioned (0): @intCast
  • Low reputation (1):
Posted by: Emmmmllll