Solved

# Logical Bit Operations

Posted on 2001-07-25
279 Views
Hi there,

I'm playing with logical Bit-Operations. I know a DWord or Integer on a 32bit Platform contains 32bit. Therefore i could pack up to 32 "booleans" into a DWord.

But yet, i was not successful to set a bit

i tried something like making constants like

const
(...)
MAX_10  = \$0000000A

and a Flag-Variable:

var
Flags: DWord;

to set a bit, i tried:

shouldn't that set a bit ?

but when i look at the value of Flags after that, it's still 0

how do i correctly set and clear bits within a DWord ?

Roger
0
Question by:omsec
• 5
• 2
• 2
• +3

LVL 11

Accepted Solution

robert_marquardt earned 100 total points
ID: 6316537
It is
to set the bit
and
Flags := (Flags and not MASK_01);
to clear the bit.

You can also get the Jedi Code Library from
It contains some bit fiddling functions.
0

LVL 20

Expert Comment

ID: 6317373
Hi Roger,

it seems you need some help with the logical operaters. Robert is right, you have to use "or", not "and". It's easy to understand why. Look at the following if statement:

result := (yourName = 'Ekelbert') and (yourNick = 'omsec');

Now we can convert that to:

result := false and true;    -> result = false

Or we can write:

result := 0 and 1;    -> result = 0

So to set a bit, you can't use "and". Do you understand that? I hope so. Here a list of the most important operators:

1010 or 1111 = 1111
1010 or 0001 = 1011

1010 and 1111 = 1010
1010 and 0001 = 0000

1010 xor 1111 = 0101
1010 xor 0001 = 1011

not 1010 = 0101
not 0001 = 1110

While operators like "+" and "-" often have effect on more than one bit (e.g. 1 + 1 = 10), all those logical operators from above have effect only bit by bit.

0

Expert Comment

ID: 6317469

DWord := 0;

// to set 1 at the second position:
DWord := DWord or  (1 shl 1);

// to set 1 at the position 5:
DWord := DWord or (1 shl 4);

//to set 0 at the position 5:
DWord := DWord xor (1 shl 4);

GunDamn
0

LVL 20

Expert Comment

ID: 6317489
>> //to set 0 at the position 5:
>> DWord := DWord xor (1 shl 4);

This is correct only if you know exactly that bit 5 was set before in the dword. If you don't know that, you should use "DWord := DWord and not (1 shl 4);". That's the better solution IMHO.
0

Expert Comment

ID: 6317543
GunDamn
0

Expert Comment

ID: 6332448
Yes, all in one you will have ("BitNo" lies between 0 and 31, both inclusively):

procedure SetBit(var Value: DWord; BitNo: Byte);
begin
Value := Value or (1 shl BitNo);
end;
procedure ClearBit(var Value: DWord; BitNo: Byte);
begin
Value := Value and not(1 shl BitNo);
end;
procedure ToggleBit(var Value: DWord; BitNo: Byte);
begin
Value := Value xor (1 shl BitNo);
end;
function GetBit(Value: DWord; BitNo: Byte): Boolean;
begin
GetBit := (Value shr BitNo) and 1 = 1;
end;

Regards, Hamilton.
0

LVL 20

Expert Comment

ID: 6333136
function GetBit(Value: DWord; BitNo: Byte): Boolean;
begin
GetBit := odd(Value shr BitNo);
end;

This should be a bit faster, I guess. Not 100% sure, though...

0

Expert Comment

ID: 6333471
You still remember that we know each other? I asked you for explaining me how to work with process and thread IDs some time ago. But I don't want to get off-topic here ... ;-)
Your improvement is very interesting, I have never had the idea to do it like that. But even if you are not 100 percent sure if it is really faster, you can ensure it is not slower. You may probably right, however, because "Odd" looks at the least significant bit to decide to return "True" or "False". That may be the same process as in my suggestion, but hopefully more efficiently in assembler.
Regards, Hamilton.
0

LVL 20

Expert Comment

ID: 6333518
Hi Hamilton,

of course I remember you, but I didn't remember from what questions exactly...

I've tested it. With Odd it is 15% faster. Well, but I had to make a 1000000 loop to get a stable result...  :-)

0

LVL 11

Expert Comment

ID: 6333578
Madshi, such optimizations are CPU dependent.
0

LVL 20

Expert Comment

ID: 6333589
Hi Robert,

generally you're right, but if you in this very case look at the assember code that Delphi makes out of those 2 functions, you'll see that the Odd variant will run faster on every CPU, it's just better asm code. If you use "and 1 = 1", you force Delphi to do exactly that. If you use "odd", Delphi is free to realize it in the most effective way. Please look at the assembler code. You'll see the difference...

0

LVL 1

Expert Comment

ID: 6908048
ADMINISTRATION WILL BE CONTACTING YOU SHORTLY.  Moderators Computer101 or Netminder will return to finalize these if still open in seven days.  Please post closing recommendations before that time.

Question(s) below appears to have been abandoned. Your options are:

1. Accept a Comment As Answer (use the button next to the Expert's name).
2. Close the question if the information was not useful to you, but may help others. You must tell the participants why you wish to do this, and allow for Expert response.  This choice will include a refund to you, and will move this question to our PAQ (Previously Asked Question) database.  If you found information outside this question thread, please add it.
3. Ask Community Support to help split points between participating experts, or just comment here with details and we'll respond with the process.
4. Delete the question (if it has no potential value for others).
--> Post comments for expert of your intention to delete and why
--> You cannot delete a question with comments, special handling by a Moderator is required.

For special handling needs, please post a zero point question in the link below and include the URL (question QID/link) that it regards with details.
http://www.experts-exchange.com/jsp/qList.jsp?ta=commspt

Please click the Help Desk link on the left for Member Guidelines, Member Agreement and the Question/Answer process for further information, if needed.  http://www.experts-exchange.com/jsp/cmtyHelpDesk.jsp

http://www.experts-exchange.com/questions/Q.11896578.html
http://www.experts-exchange.com/questions/Q.20109588.html
http://www.experts-exchange.com/questions/Q.20154494.html
http://www.experts-exchange.com/questions/Q.20156716.html
http://www.experts-exchange.com/questions/Q.20237072.html

http://www.experts-exchange.com/questions/Q.20236974.html

PLEASE DO NOT AWARD THE POINTS TO ME.

------------>  EXPERTS:  Please leave any comments regarding your closing recommendations if this item remains inactive another seven (7) days.  Also, if you are interested in the cleanup effort, please click this link http://www.experts-exchange.com/jsp/qManageQuestion.jsp?ta=commspt&qid=20274643

Moderators will finalize this question if still open in 7 days, by either moving this to the PAQ (Previously Asked Questions) at zero points, deleting it or awarding expert(s) when recommendations are made, or an independent determination can be made.  Expert input is always appreciated to determine the fair outcome.

Thank you everyone.

Moondancer
Moderator @ Experts Exchange
0

LVL 1

Expert Comment

ID: 6976054
ADMINISTRATION WILL BE CONTACTING YOU SHORTLY.  Moderators Computer101 or Netminder will return to finalize these if they are still open in 14 days.  Experts, please post closing recommendations before that time.

Below are your open questions as of today.  Questions which have been inactive for 21 days or longer are considered to be abandoned and for those, your options are:
1. Accept a Comment As Answer (use the button next to the Expert's name).
2. Close the question if the information was not useful to you, but may help others. You must tell the participants why you wish to do this, and allow for Expert response.  This choice will include a refund to you, and will move this question to our PAQ (Previously Asked Question) database.  If you found information outside this question thread, please add it.
3. Ask Community Support to help split points between participating experts, or just comment here with details and we'll respond with the process.
4. Delete the question (if it has no potential value for others).
--> Post comments for expert of your intention to delete and why
--> YOU CANNOT DELETE A QUESTION with comments; special handling by a Moderator is required.

For special handling needs, please post a zero point question in the link below and include the URL (question QID/link) that it regards with details.
http://www.experts-exchange.com/jsp/qList.jsp?ta=commspt

Click you Member Profile to view your question history and please keep them updated. If you are a KnowledgePro user, use the Power Search option to find them.

Questions which are LOCKED with a Proposed Answer but do not help you, should be rejected with comments added.  When you grade the question less than an A, please comment as to why.  This helps all involved, as well as others who may access this item in the future.  PLEASE DO NOT AWARD POINTS TO ME.

http://www.experts-exchange.com/questions/Q.20109588.html
http://www.experts-exchange.com/questions/Q.20154494.html
http://www.experts-exchange.com/questions/Q.20156716.html
http://www.experts-exchange.com/questions/Q.20237072.html
http://www.experts-exchange.com/questions/Q.11896578.html

http://www.experts-exchange.com/questions/Q.20236974.html

*****  E X P E R T S    P L E A S E  ******  Leave your closing recommendations.
If you are interested in the cleanup effort, please click this link
http://www.experts-exchange.com/jsp/qManageQuestion.jsp?ta=commspt&qid=20274643
http://www.experts-exchange.com/commspt/Q.20277028.html

Moderators will finalize this question if in @14 days Asker has not responded.  This will be moved to the PAQ (Previously Asked Questions) at zero points, deleted or awarded.

Thanks everyone.
Moondancer
Moderator @ Experts Exchange
0

LVL 26

Expert Comment

ID: 8732705
No comment has been added lately, so it's time to clean up this TA.
I will leave a recommendation in the Cleanup topic area that this question is:

To accept robert_marquardt's comment as answer

Thank you,
Russell

EE Cleanup Volunteer
0

## Featured Post

Objective: - This article will help user in how to convert their numeric value become words. How to use 1. You can copy this code in your Unit as function 2. than you can perform your function by type this code The Code Â  (CODE) The Imâ€¦