Make up for the next step of hook learning: how to extract one of the 32 bits

Source: Internet
Author: User
The integer type is 32-bit and has 4 bytes. Now we need to be able to extract one of its 32-bit values.
But the smallest integer type of Delphi is also a byte (8 bits): byte (unsigned), struct int (Signed ).
Otherwise, start with extracting a byte:
VaR I: integer; B: byte; begin I: = maxint; {maximum integer} showmessage (inttostr (I); {2147483647} {the binary representation of I is: 01111111 11111111 11111111 11111111} {the highest bit of interger 0 indicates that this is a positive number (1 indicates a negative number)} {suppose:} I: = 2146439167; {now the binary representation of I is: 01111111 11110000 00001111 11111111} {now its hexadecimal representation is: $7 F 0 0 f} {implement it, from right to left, the four bytes are: $ ff, $ 0f, $ F0, $ 7f} {if you need to extract one of the four bytes separately, we provide two functions for the Delphi bit:} B: = Lo (I ); {extract low byte} showmessage (Format ('%. 2x ', [B]); {ff; this is the first byte from the right} B: = Hi (I); {extract high byte} showmessage (format (' %. 2x ', [B]); {0f; this is the second byte from the right} {so how do we extract the third and fourth byte? Method 1:} {shifted right 16 bits, and then extracted with Lo and HI} {01111111 11110000 00001111 11111111 shifted right 16 bits, it will become:} {01111111 11110000; try:} B: = Lo (I SHR 16); showmessage (format ('%. 2x ', [B]); {F0; this is the third byte from the right} B: = Hi (I SHR 16); showmessage (format (' %. 2x ', [B]); {7f; this is the fourth byte from the right} {of course, the fourth byte of I can also be extracted as follows:} B: = Lo (I SHR 24); showmessage (format ('%. 2x ', [B]); {7f; this is the fourth byte from the right} {now let's change our mind. If there are no lo or hi functions, can we do it? Certainly yes:} B: = (I and $ ff); showmessage (format ('%. 2x ', [B]); {ff; this is the first byte from the right} B: = (I SHR 8 and $ ff); showmessage (format (' %. 2x ', [B]); {0f; this is the second byte from the right} B: = (I SHR 16 and $ ff); showmessage (format (' %. 2x ', [B]); {F0; this is the third byte from the right} B: = (I SHR 24 and $ ff); showmessage (format (' %. 2x ', [B]); {7f; this is the fourth byte from the right} {why? Change the statement block for careful analysis} end;
 
   
 
 
// Supplement the above example: var B: byte; begin {we know that the maximum value of byte is 255, that is, the hexadecimal $ ff, binary 11111111} {This $ FF has such a special purpose:} B: = 0 and $ ff; showmessage (inttostr (B); {0} B: = 1 and $ ff; showmessage (inttostr (B); {1} B: = 100 and $ ff; showmessage (inttostr (B); {100} B: = 255 and $ ff; showmessage (inttostr (B); {255} {0 .. 255 After performing the and operation on any number and $ FF directly, the value remains unchanged (this is not difficult to understand from the binary perspective)} {In addition, byte is a byte. When you give it more, it only needs one byte (low byte), such as:} B: = byte (maxint); showmessage (inttostr (B )); {255} {the above example should be understandable} end;

  

Back to topic:

// In this example, we call the percentile bit 0th bits and the maximum integer bits 31st bits. vaR I: integer; B: byte; begin {use the value in the first example:} I: = 2146439167; {the binary representation of I is: 01111111 11110000 00001111 11111111} {the maximum value of a single byte is $ ff; the maximum value of a binary bit is of course 1, written as hexadecimal or $1} {extract 0th bits:} B: = I and 1; showmessage (inttostr (B); {1} {extract 0th digits can also be written (0 digits to the right is not moved):} B: = I shr 0 and 1; showmessage (inttostr (B); {1} {extract 1st bits:} B: = I SHR 1 and 1; showmessage (inttostr (B); {1} {extract 13th bits:} B: = I shr 13 and 1; showmessage (inttostr (B )); {0} {extract the highest bit (31st bits):} B: = I shr 31 and 1; showmessage (inttostr (B); {0} end;

  
 
// If you only judge the highest bit, whether it is 0 or 1 (that is, whether it is positive or negative), you can also: var I: integer; begin I: = maxint; {this must be a positive number} If I shr 31 = 0 then showmessage ('position'); {positive} I: =-1; if I SHR 31 = 1 then showmessage ('negative '); {negative} 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.