0

Depending on a variable 1-7, I am interested in ANDing a byte value to to get that number of bits.

Currently I'm doing this:

function getMaskedBits(num, Bit_location: Byte): Byte;
var
  masks: array[1..7]of Byte=(127,63,31,15,7,3,1);
begin
  Result:=num and masks[Bit_location];
end;

is there a better (or more efficient way of doing this?)

Edited by pritaeas: Added code tags

2
Contributors
2
Replies
14
Views
4 Years
Discussion Span
Last Post by davecoventry
0

This works just fine.

You can probably also use something like this:

Result := num and (127 shr (bit_location - 1));

Edited by pritaeas

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.