We're a community of 1076K IT Pros here for help, advice, solutions, professional growth and fun. Join us!
1,075,725 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Start New Discussion Reply to this Discussion

Generating a bitwise AND filter.

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?)

2
Contributors
2
Replies
1 Week
Discussion Span
3 Months Ago
Last Updated
14
Views
Related Article: May I ask you a question on Delphi? is a solved Pascal and Delphi discussion thread by turbomen that has 4 replies and was last updated 8 months ago.
davecoventry
Light Poster
43 posts since Jun 2009
Reputation Points: 10
Solved Threads: 0
Skill Endorsements: 0

This works just fine.

You can probably also use something like this:

Result := num and (127 shr (bit_location - 1));
pritaeas
Posting Prodigy
Moderator
9,265 posts since Jul 2006
Reputation Points: 1,173
Solved Threads: 1,456
Skill Endorsements: 86

Sorry for not replying earlier: Thanks very much for your help!

davecoventry
Light Poster
43 posts since Jun 2009
Reputation Points: 10
Solved Threads: 0
Skill Endorsements: 0

This article has been dead for over three months: Start a new discussion instead

Post: Markdown Syntax: Formatting Help
 
You
 
© 2013 DaniWeb® LLC
Page rendered in 0.5363 seconds using 2.47MB