Bit Delphi XML Manipulation - Bitwise

I am a high school student and am currently studying in Delphi XE3. We study the manipulation of BIT. We have a task, and although I read a lot on this issue and understand the whole process of storing information in bits and SHL / SHR, I hardly understand how to do this in Delphi.

The purpose is as follows:

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

Passing an integer value in an XML file to determine installed options. For instance. If I wanted to send option 1 and option 2, I would add 1 + 2 = 3. I would send 3 as a number to indicate that parameters 1 and 2. are true.

On the client, the binary value will be 0000000000000011 = 3

From what I read, I need to use a mask, but I don’t understand how to do it. How to use masks in Delphi to get individual values ​​that would be True or False.

I tried to do this in a regular Integer variable, but it is always treated as a whole, and the result is very strange. If I convert an integer to a binary string representation and I repeat its characters, the result will be correct, but I assume that I should not do this with strings. Any help or example would be greatly appreciated. Thanks.

+4
source share
3 answers

Usually you check if a specific bit is set in the Integer variable using the binary operator and , and you set the individual bits using the or operator, for example:

 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 various OPTION_ constants are actually masks , in the sense that they are used to mask bits to zero (to check bits) or to mask bits to 1 (to set a specific bit).

Consider this snippet:

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

or mask the first bit to 1. If we start with the parameter value (in binary terms) 01010000, then the parameters will be 01010001 as a result

  01010000 OR 00000001 //OPTION_X = 01010001 

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

  • first, it starts all uninterested bytes of the Option variable at 0. If we look at the last value 01010001, the operation will clear all the bits, but the first.

      01010001 AND 00000001 = 00000001 

given the initial value 01010000, it will return zero:

  01010000 AND 00000001 = 00000000 
  • next, it compares if this 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, as far as taste is concerned, you can simply check if the resulting value, for example, is different from 0, but if your mask contains several bits, and you want to check if all bits have been set, you should check equality.
+3
source

Delphi has a predefined type TIntegerSet that allows you to use set operators. Assuming options is an integer, you can check if any bit (based on 0) is set as follows:

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

Changing parameters is performed using 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 useful.

+2
source

Delphi has Bitwise operators for controlling individual bits of integer types. Look at the shl , shr , and , or and xor statements. To combine bits, use the or operator. To check the bit, 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 returns an output value that has bit 1 in those places where either the input value has bit 1 . Thus, the union of the bits will 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 returns an output value that has bit 1 only in those places where both input values ​​have bit 1 , otherwise it creates bit 0 instead. Therefore, bit testing should 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; 
+1
source

All Articles