5

I am a student in high school and I am currently learning in Delphi XE3. We are learning about BIT manipulation. We have an assignment and while I have read a lot on the subject and understand the entire process of storing information in Bits and SHL/SHR I am having difficulty understanding how to do this process in Delphi.

The assignment is as follows:

Decimal        Hexidecimal    Binary
1              0x0001         0000000000000001
2              0x0002         0000000000000010
4              0x0004         0000000000000100

Passing an integer value in an XML file to identify the options set. For example. If I wanted to send option 1 and option 2, I would add 1+2=3. I would send 3 as the number to specify that options 1 and 2 are true.

On the client the binary value would be 0000000000000011 = 3

From what I have read I need to use a mask but I do not understand how to do this. How would do I use masks in Delphi ot obtain the individual values which would be True or False.

I tried doing this in a regular Integer variable but it always gets treated as an Integer and the result is very strange. If I convert the integer to a binary string representation and I iterate thru the characters the result is correct but I am assuming that I should not be doing this with strings. Any help or an example would be greatly appreciated. Thank you.

6
  • Please can you ask a specific question. There isn't one here. Jan 23, 2013 at 19:10
  • I wanted to understand how to read if a particular bit is set from an integer value and how to set a particular bit. If value is 1 + 4 = 5 then how can I read if the 1 and 3 bits from the right are set or equal to True.
    – LHearn
    Jan 23, 2013 at 19:46
  • Well, we generally number bits from 0. So you'd be testing bits 0 and 2. And to do that you write (x and 1)<>0 for bit 0, and (x and 4)<>0 for bit 2. You should have asked that question more directly. Jan 23, 2013 at 19:49
  • X being a regular Integer or Byte variable?
    – LHearn
    Jan 23, 2013 at 19:59
  • @DavidHeffernan, Thank you. What would I use SHL and SHR for?
    – LHearn
    Jan 23, 2013 at 20:02

3 Answers 3

3

You usually check if a particular bit is set in a Integer variable using the and binary operator, and you set individual bits using the or operator, like this:

const
  OPTION_X = $01;
  OPTION_Y = $02;
  OPTION_Z = $04;

var
  Options: Byte;
begin
  Options := OPTION_X or OPTION_Y;  //actually 3, like in your example
  //check if option_X is set
  if (Options and OPTION_X) = OPTION_X then
    ShowMessage('Option X is set');  //this message is shown, because the bit is set
  //check if option_Z is set
  if (Options and OPTION_Z) = OPTION_Z then
    ShowMessage('Option Z is set');  //this message is NOT shown
end;

The different OPTION_ constants, are actually masks, in the sense they are used to mask bits to zero (to check if a particular bit is set) or to mask bits to 1 (to set a particular bit).

Consider this fragment:

begin
  ..

  if cbOptionX.Checked then
    Options := Options or OPTION_X;
  ..

the or will mask the first bit to 1. If we start with a Options value (in binary) of 01010000, the resulting Options would be 01010001

    01010000   
 OR 00000001  //OPTION_X 
  = 01010001   

the same value is used to mask all the other bits to 0 to check if a particular bit is set. The if condition, for example: (Options and OPTION_Z) = OPTION_Z, does this:

  • first it MASKS all the non-interesting bytes of the Option variable to 0. If we consider the last value of 01010001, the operation will result in clearing all the bits, but the first.

        01010001   
    AND 00000001   
      = 00000001   
    

considering a starting value of 01010000 it will return zero:

        01010000   
    AND 00000001   
      = 00000000   
  • next, it compares if that value is equal to the mask itself. If it is equal, the bit was set in the original Options variable, otherwise it was not set. If your mask contains only one bit, that's matter of taste, you can just check if the resulting value is, for example, different than 0, but if your mask contains multiple bits and you want to check if all the bits was set, you have to check for equality.
14
  • 1
    While all answers were helpful and UWA Rabee gave the simplest answer, I needed to understand the concept of masks. @jachguate, thank you very much. Much simpler than I thought.
    – LHearn
    Jan 23, 2013 at 19:48
  • Far better to test <>0 and so avoid violating DRY Jan 23, 2013 at 19:50
  • I read the code that tested for single bits. For which my comment applies. Jan 23, 2013 at 19:56
  • @jachguate, Can I check for multiple bits in one operation?
    – LHearn
    Jan 23, 2013 at 20:08
  • 1
    @LHearn your if looks good. Why do you want to iterate trough all bit values?
    – jachguate
    Jan 23, 2013 at 21:01
2

Delphi has a predefined type TIntegerSet which allows to use set operators. Assuming that options is an Integer, you can check if any bit (0-based) is set like this:

option1 := 0 in TIntegerSet(options); { Bit 0 is set? }
option3 := 2 in TIntegerSet(options); { Bit 2 is set? }

Changing the options is done via Include or Exclude:

Include(TIntegerSet(options), 0); { set bit 0 }
Exclude(TIntegerSet(options), 2); { reset bit 2 }

Of course you can use any other set operator that may be helpful.

1

Delphi has Bitwise Operators for manipulating individual bits of integer types. Look at the shl, shr, and, or, and xor operators. To combine bits, use the or operator. To test for bits, use the and operator. For example, assuming these constants:

const
  Option1 = 0x0001;
  Option2 = 0x0002;
  Option3 = 0x0004;

The or operator looks at the bits of both input values and produces an output value that has a 1 bit in places where either input value has a 1 bit. So combining bits would look like this:

var
  Value: Integer;
begin
  Value := Option1 or Option2;
  {
  00000000000000000000000000000001 Option1
  00000000000000000000000000000010 Option2
  -------------------------------- OR
  00000000000000000000000000000011 Result
  }
  ...
end;

The and operator looks at the bits of both input values and produces an output value that has a 1 bit only in places where both input value have a 1 bit, otherwise it produces a 0 bit instead. So testing for bits would look like this:

var
  Value: Integer;
  Option1Set: Boolean;
  Option2Set: Boolean;
  Option3Set: Boolean;
begin
  Value := 7; // Option1 or Option2 or Option3

  Option1Set := (Value and Option1) = Option1;
  {
  00000000000000000000000000000111 Value
  00000000000000000000000000000001 Option1
  -------------------------------- AND
  00000000000000000000000000000001 Result
  }

  Option2Set := (Value and Option2) = Option2;
  {
  00000000000000000000000000000111 Value
  00000000000000000000000000000010 Option2
  -------------------------------- AND
  00000000000000000000000000000010 Result
  }

  Option3Set := (Value and Option3) = Option3;
  {
  00000000000000000000000000000111 Value
  00000000000000000000000000000100 Option3
  -------------------------------- AND
  00000000000000000000000000000100 Result
  }
  ...
end;

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.