Next lesson for hooks: How to extract one of the 32 digits

Source: Internet
Author: User

The Integer type is 32-bit and has 4 bytes, and now we need to be able to extract one of its 32-bit digits.

But the smallest integer type of Delphi is also a byte (8 bits): Byte (unsigned), Shortint (signed).

Start by extracting a byte first:

var


I:integer;


B:byte;


begin


I: = Maxint; {Integer maximum}


ShowMessage (IntToStr (i)); {2147483647}


{Now I binary representation is: 01111111 11111111 11111111 11111111}


{Interger's highest bit of 0 means that this is a positive number (1 is a negative number)}





{if:}


I: = 2146439167;


{Now I binary representation is: 01111111 11110000 00001111 11111111}


{Now its hexadecimal representation is: $ f F 0 0 F f F}


{implement, four bytes from right to left are: $FF, $0f, $F 0, $7f}





{If you need to extract one byte of four bytes separately, Delphi bit we provide two functions:}





B: = Lo (i); {Extract Low Byte}


showmessage (Format ('%.2x ', [b])); {FF; This is from the right of the first byte}





B: = Hi (i); {Extract High Byte}


ShowMessage (Format ('%.2x ', [b])); {0F; This is from the right number two bytes}





{So how do we extract the third and fourth bytes? Method one:}


{Move 16 digits to the right and then use Lo and Hi to extract}


{01111111 11110000 00001111 11111111 Right-shift 16-bit will become:}


{01111111 11110000; try it:}





B: = Lo (i shr 16);


showmessage (Format ('%.2x ', [b])); {F0; This is from the right number three bytes}


B: = Hi (i shr 16);


showmessage (Format ('%.2x ', [b])); {7F; This is from the right number four bytes}





{Of course i's fourth byte can also be extracted like this:}


B: = Lo (i shr 24);


showmessage (Format ('%.2x ', [b])); {7F; This is from the right number four bytes}








{Now, if there is no Lo and Hi function, can we do it? Of course:}


B: = (I and $FF);


showmessage (Format ('%.2x ', [b])); {FF; This is from the right of the first byte}


B: = (i shr 8 and $FF);


showmessage (Format ('%.2x ', [b])); {0F; This is from the right number two bytes}


B: = (i shr and $FF);


ShowMessage (Format ('%.2x ', [b])); {F0; This is from the right number three bytes}


B: = (i shr and $FF);


showmessage (Format ('%.2x ', [b])); {7F; This is from the right number four bytes}





{This is why?) to change the block of statements to carefully analyze}


end;





//Additions to the above examples:


var


B:byte;


begin


{We know that the maximum Byte value is 255, which is hexadecimal $FF, binary 11111111}


{This $FF has a special purpose:}


B: = 0 and $FF;


ShowMessage (IntToStr (b)); {0}


B: = 1 and $FF;


ShowMessage (IntToStr (b)); {1}


B: = $FF;


ShowMessage (IntToStr (b)); {100}


B: = 255 and $FF;


ShowMessage (IntToStr (b)); {255}


{0..255 any number immediately after and with the $FF, the value is unchanged (this is not difficult to understand from a binary perspective)}





{Additionally, byte is a byte, and when you give it more, it also takes a byte (low byte), for example:}


B: = Byte (maxint);


ShowMessage (IntToStr (b)); {255}





{The above example should be understood now}


end;

Back to topic:

//本例中我们把最低位叫第 0 位; 把 Integer 的最高位叫第 31 位.
var
i: Integer;
b: Byte;
begin
{还是用第一个例子中的值吧:}
i := 2146439167;
{现在 i 的二进制表示是: 01111111 11110000 00001111 11111111}

{一个字节的最大值是 $FF; 一个二进制位的最大值当然是 1, 写成十六进制还是 $1 }
{提取第 0 位:}
b := i and 1; ShowMessage(IntToStr(b));    {1}
{提取第 0 位也可以写作(右移0位就是没动):}
b := i shr 0 and 1; ShowMessage(IntToStr(b)); {1}

{提取第 1 位:}
b := i shr 1 and 1; ShowMessage(IntToStr(b)); {1}

{提取第 13 位:}
b := i shr 13 and 1; ShowMessage(IntToStr(b)); {0}

{提取最高位(第 31 位):}
b := i shr 31 and 1; ShowMessage(IntToStr(b)); {0}
end;

//假如只判断最高位, 是 0 还是 1(也就是判断正负), 还可以这样:
var
i: Integer;
begin
i := MaxInt; {这肯定是个正数}
if i shr 31 = 0 then ShowMessage('正'); {正}

i := -1;
if i shr 31 = 1 then ShowMessage('负'); {负}
end;

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.