Delphi. Bit operation

Source: Internet
Author: User
Tags constant definition

Bit operation Online has a lot of introduction, please Internet Google/baidu, such as:

Bit operation Skill Example Daquan:

http://blog.csdn.net/g_spider/article/details/5750665

A comprehensive summary of the position Operation basics

http://blog.csdn.net/morewindows/article/details/7354571

。。。

The bit operation C + + or delphi/pascal, in fact, are similar, but the language expression is different.

-bit operation C + + delphi/pascal

With & and

or | Or

Non -! Not

Xor ^

Shift left << SHL

Shift Right >> SHR

Note that the bitwise operation (and/or non) in D is the same as the keyword of the logical operation, so you may be mistaken if you do not notice it.

Below, the introduction of my usual bit operation, other skills, please check the information yourself:

One: Flag bit: And or not

The flag bit has a value : V: = V or flag;

Flag bit No value : V: = V and not flag;

Check if a flag bit has a value: if (V and flag) = Flag then has value else no value.

which

1:v = Integer/int64, which is a 32-or 64-bit number (non-floating-point type)

The 2:flag must be 2 of the n-th party, (please use 16 binary 8421 mnemonic), namely:

$, $, $4, $8,

$ A, $ $, $, $

$, $, $, $800

...

$10000000, $20000000, $40000000, $80000000

are similar to 16, and if there are "n bits" for the set value, then flag = $ or $, which is similar.

Then, seeing these, if you often look at the constant definition of Windows.pas, you will find that many constants are defined in this way.

For that, this flag bit is evaluated, assigned, and checked.

Specifically why this definition, please use the calculator (binary mode), for and or not operation, slowly touch: D

3:if (V and flag) = Flag then has value else no value.

In general, I will omit this as:

If (V and flag) <> 0 then a value of the flag bit has value ;

If (V and flag) = 0 Then the flag bit of a value has no value ;

Then, you can write a multi-flag bit:

Const MASKS = Flag1 or Flag2 or flag3 ...;

Multiple flags: x: = V or masks;

Value multi-flag: x: = V and not masks;

Check multi-flag: If x and masks <> 0 then ...

Two: byte alignment:

BYTE alignment---to a value, which is done by: the insufficiency is the complement. Such as:

Give a number: X, to make a 4 complement to it, then:

1:x = 5 o'clock, need to get align (x) = 8, that is, 8 is x to 4 after the completion of the number.

2:x = 16 o'clock, has been completed, then align (x) = 16, that is, maintain the original value.

Note: The alignment number must be 2 of the n-th square.

The above logic, written in code, is:

x: = 5;

A: = 4;

x: = (x + (A-1)) and not (A-1);

So, after the above line, and not operation, X is aligned with a.

I usually do this:

x: = (x + a-1) and-a;

Three: Multiplication method

Displacement can be limited to replace the multiplication, because only for the number of multiplication is 2 of the n-th side, such as:

X * 2 ==> x SHL 1

X * 4 ==> x SHL 2

X Div 8 ==> x shr 3

X Div 4 ==> x shr

See the number of multiplication: 2, 4, 8, 16, equivalent to 2 1,2,3,4.

  

Four: Mask Correlation

Take the first 4 bits in a byte: V: = V and $0f;

Take the last 4 bits in one byte: V: = v shr $04;

The first 3 bits, that is, the first three bits (in binary numbers) as a mask, and then do and:flag = $01 or $02 or $04; V: = V and flag;

After 3, I thought about it.

Five: According to the three + four way, can be very convenient to use and for the mold operation

The modulo operation uses the hash index value to be: mod:index: = ABS (key mod len);

You can also do this:

1:len = 2 of the n-th square.

2:index: = key and (len-1);

Six: Some tips:

1: Check if a value is 2 N-square: if (V and (V-1)) = 0 Then Writeln (' V is the n-th ' of 2);

2: Detects if a value is even: if (V and 1) = 0 Then Writeln (' V is even '); Equivalent to: if v mod 2 = 0

3: Think again, some are not used, or please go to the above link to see some more detailed tips.  

The level is limited, if has the similarity, is hotlinking,:D

2014.11.02 by QSL

Delphi. Bit operation

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.