Bitmask - Help

BlitzMax Forums/BlitzMax Programming/Bitmask - Help

VinceA(Posted 2007) [#1]
If there is anybody familiar with C# who could help me translate the below code into BlitzMax code, I would eternally grateful.. I’m kind of lost!!


public struct Bitmask
{
public int mask;

public void clear() { mask = 0x00; }
public void setOn(int bit)
{
mask |= (0x01 << ((bit > 0) ? (bit - 1) : 0));
}
public void setOff(int bit)
{
mask &= ~(0x01 << ((bit > 0) ? (bit - 1) : 0));
}
}


JazzieB(Posted 2007) [#2]
I'm no C# expert, never tried it, but from studying the above code, the following should do the same.

Type Bitmask

	Field mask:Int

	Method Clear()
		mask=0
	EndMethod

	Method SetOn(bit:Int)
		mask:|(1 Shl (bit-1))
	EndMethod

	Method SetOff(bit:Int)
		mask:&(~(1 Shl (bit-1)))
	EndMethod

EndType

For an Int, bits are numbered from 1-32, from right to left, i.e. LSB to MSB.

The C# code does have a little bit of checking in there also, to make sure that bit is > 0, but I don't think there's any need for this as long as you don't pass silly values to it in the first place.


VinceA(Posted 2007) [#3]
Thanks a bunch!!
It Works!