Solved

Logical Bit Operations

Posted on 2001-07-25
15
279 Views
Last Modified: 2010-04-06
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
  MASK_01 = $00000001
  (...)
  MAX_10  = $0000000A

and a Flag-Variable:

var
  Flags: DWord;

to set a bit, i tried:

Flags := (Flags and MASK_01);

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
Comment
Question by:omsec
  • 5
  • 2
  • 2
  • +3
15 Comments
 
LVL 11

Accepted Solution

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

You can also get the Jedi Code Library from
http://delphi-jedi.org link 'Code Library'.
It contains some bit fiddling functions.
0
 
LVL 20

Expert Comment

by:Madshi
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.

Regards, Madshi.
0
 

Expert Comment

by:comptebidon81
ID: 6317469
To set your 32 bits into your DWord, do something like:

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

by:Madshi
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

by:comptebidon81
ID: 6317543
oops sorry, your right Madshi...
GunDamn
0
 

Expert Comment

by:Hamilton
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

by:Madshi
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...

Regards, Madshi.
0
How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

 

Expert Comment

by:Hamilton
ID: 6333471
Hi Madshi!
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

by:Madshi
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...  :-)

Regards, Madshi.
0
 
LVL 11

Expert Comment

by:robert_marquardt
ID: 6333578
Madshi, such optimizations are CPU dependent.
0
 
LVL 20

Expert Comment

by:Madshi
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...

Regards, Madshi.
0
 
LVL 1

Expert Comment

by:Moondancer
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

Click you Member Profile to view your question history and keep them all current with updates as the collaboration effort continues, to track all your open and locked questions at this site.  If you are an EE Pro user, use the Power Search option to find them.  Anytime you have questions which are LOCKED with a Proposed Answer but does not serve your needs, please reject it and add comments as to why.  In addition, when you do grade the question, if the grade is less than an A, please add a comment as to why.  This helps all involved, as well as future persons who may access this item in the future to seek help.

To view your open questions, please click the following link(s) and keep them all current with updates.
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


To view your locked questions, please click the following link(s) and evaluate the proposed answer.
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

by:Moondancer
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
 
Please click this link for Help Desk, Guidelines/Member Agreement and the Question/Answer process.  http://www.experts-exchange.com/jsp/cmtyHelpDesk.jsp

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.

To view your open questions, please click the following link(s) and keep them all current with updates.
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

To view your locked questions, please click the following link(s) and evaluate the proposed answer.
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
POINTS FOR EXPERTS awaiting comments are listed in the link below
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

by:Russell Libby
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

Please leave any comments here within the next seven days.
 
PLEASE DO NOT ACCEPT THIS COMMENT AS AN ANSWER!
 
Thank you,
Russell

EE Cleanup Volunteer
0

Featured Post

Threat Intelligence Starter Resources

Integrating threat intelligence can be challenging, and not all companies are ready. These resources can help you build awareness and prepare for defense.

Join & Write a Comment

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…
Have you ever had your Delphi form/application just hanging while waiting for data to load? This is the article to read if you want to learn some things about adding threads for data loading in the background. First, I'll setup a general applica…
In this seventh video of the Xpdf series, we discuss and demonstrate the PDFfonts utility, which lists all the fonts used in a PDF file. It does this via a command line interface, making it suitable for use in programs, scripts, batch files — any pl…
This video demonstrates how to create an example email signature rule for a department in a company using CodeTwo Exchange Rules. The signature will be inserted beneath users' latest emails in conversations and will be displayed in users' Sent Items…

706 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

19 Experts available now in Live!

Get 1:1 Help Now