enums - Perform addition on bit-fields in C# -
i working on gomoku implementation in c#, , first challenge of project intelligently represent state of board.
in order achieve goal, decided turn bit-fields represent value of combination:
[flags] public enum combinationvalue : byte { onepawn = 1, twopawns = 2, threepawn = 4, fourpawn = 8, fivepawns = 16 }
the problem stumbled on, when want increment combination, want unset previous bit , set new one, binary shift left, not allowed enum. example, let's have combination of 2 pawns, , pawn put next value of enum must become "threepawn" instead of "twopawns".
so basically, want twopawns + onepawn == threepawns.
what efficient way implement behavior?
this isn't how flags should used. flags used designate switches in single byte, given binary 0000
first bit represents element being "on", second different element, etc.
so element1 represented by: 1 = 0001
, element2: 2= 0010
, element3: 4 = 0100
, etc. turn element 1 , element 2 on use binary 0011
or 3
(1+2
).
fyi; dates days when computers had limited memory , being able represent multiple states in small number of bits advantagous.
what want doesn't make sense binary point of view, 3 prawns 0011
or switch 1 , 2. isn't "state". switches for.
if want designate number of prawns , perfrom addition, what's wrong old fashined int
?
Comments
Post a Comment