Hi,
I have problems with bitshifting:
const std = @import("std");
pub const BitBoardState = u64;
pub const Square = enum(u8) { a1, b1, c1, d1, e1, f1, g1, h1, a2, b2, c2, d2, e2, f2, g2, h2, a3, b3, c3, d3, e3, f3, g3, h3, a4, b4, c4, d4, e4, f4, g4, h4, a5, b5, c5, d5, e5, f5, g5, h5, a6, b6, c6, d6, e6, f6, g6, h6, a7, b7, c7, d7, e7, f7, g7, h7, a8, b8, c8, d8, e8, f8, g8, h8, undefined = 255 };
pub fn is_occupied(board: BitBoardState, square: Square) bool {
const state: BitBoardState = @as(u64, 1) << @as(u8, @intFromEnum(square));
return (board & state) == state;
}
pub fn main() !void {
const bitboard: BitBoardState = 0;
const occupied = is_occupied(bitboard, Square.a1);
std.debug.print("Is occupied {any}", .{occupied});
}
This fails with following error:
prog.zig:9:49: error: expected type ‘u6’, found ‘u8’
const state: BitBoardState = @as(u64, 1) << @as(u8, @intFromEnum(square));
^~~~~~~~~~~~~~~~~~~~~~~~~~~~~
prog.zig:9:49: note: unsigned 6-bit int cannot represent all possible unsigned 8-bit values
referenced by:
main: prog.zig:16:33
posixCallMainAndExit: zig/lib/std/start.zig:660:37
4 reference(s) hidden; use ‘-freference-trace=6’ to see all references
When I change this to:
pub fn is_occupied(board: BitBoardState, square: Square) bool {
const state: BitBoardState = @as(u64, 1) << @intCast(@intFromEnum(square));
return (board & state) == state;
}
then it works.
Why does @intCast magically fix the problem?
Thanks in advance!