About << when translating code from c to zig

I m trying to do an equivalent zig program from c.
In the c program i have this…
len = (buf[0] << 8) | buf[1];

In the zig version i read the first byte to left: u8 and second byte to right: u8
and trying this

zig build
./src/main.zig:14:33: error: integer value 8 cannot be coerced to type ‘u3’
const length = (left << 8) | right;

Any hint?

From what I understand, the compiler is saying that if left is a u8, then the most you can left shift is what can fit in a u3 which is 0 to 7. That 8 literal you’re passing as the right hand side of << is a comptime_int and Zig tries to coerce it into the u3 but it doesn’t fit. I think left has to be bigger than a u8 then.

Makes sense, thanks a lot! I wonder how is possible to do that in C.

shifting a u8 by 8 (or more) (in a best case scenario where you dont get garbage) is always going to be 0.

and many compilers (llvm in this case) forbid it because there’s only 8 bits to shift in the first place. C is much less type safe

I believe it’s because in C it automatically converts values to type int before performing operations on them like arithmetic shift.