Complement, original code, inverse code-shift operation coding and decoding

Source: Internet
Author: User

Data is stored in bytes on the computer, 1 bytes equals 8 bits (1byte=8bit), and the computer can recognize only 0 and 1 of these two digits, so 1 bytes can represent 256 different information according to the arrangement, i.e. 28 (0 and 12 possible, 8-bit arrangement).

Integers (4Byte) are divided into: signed and unsigned integers. The highest level in the computer store represents a sign bit, and if the highest digit is 0 to indicate a negative number, the top digit is 1 to indicate a positive number. The negative integer is stored in the computer in the complement form, how does the complement be expressed, here also introduce another concept---------code, the so-called inverse code is the original code of negative numbers (negative source code and its absolute value of the corresponding original code is the same as the absolute value of the same number of original code is the same) each bit by bit to reverse, is 1 to replace 0, is 0 to replace 1, such as-1 of the original code is 00000001, and 1 of the original code is the same, then-1 of the inverse code is 11111110, and the complement is on the basis of the inverse code plus 1, that is, 1 of the complement is 11111110+1= 11111111, so we can figure out that 1 is stored on the computer by 11111111. To sum up, the computer stores signed integers, is used to store the complement of the integer, 0 of the original code, the complement is 0, a positive source code, the complement can be specifically interpreted as the same, minus the complement is its inverse code plus 1.

In Java, a shift in Java numerical computation, if it is Short,char,byte, is converted to an int in the form of a shift.

How to: 1.) byte a = 27;//converted to int 00000000000000000000000000011011
byte B =-1; convert to int 11111111111111111111111111111111
int g = a >> 1;

Sign right 1-bit, left missing bit to be filled with sign bit, positive number is 0, "00000000000000000000000000001101" = 13int f = b>> 1;

Sign right 1-bit, left missing bit to be filled with sign bit, minus is 1, "11111111111111111111111111111111" =-1
So when you print it out, g=13,gf=-1.
g = a >>> 1;

Unsigned right 1-bit, left-missing bit to be padded with 0, "00000000000000000000000000001101" = 13f = b>>> 1;

Unsigned right 1-bit, left missing bit 0 padded, "01111111111111111111111111111111" = 2147483647
So when you print it out, g=13,gf=2147483647.
f = b<< 1;

Unsigned left 1 digits, the right missing bit is padded with 0, "10000000000000000000000000000010" =-2 So when printed, f=-2
In the thinking in Java Chapter Three: The bitwise operator is also a binary "bit". You can use them individually to handle integer types (one of the main types).

The left shift operator (<<) is able to move the operand on the left side of the operator to the right of the specified number of digits on the right-hand side of the operator (0 in low).

The signed right shift operator (>>) assigns the Operation object to the left of the operator to the right of the specified number of digits to the right-hand operator.

The "signed" right shift operator uses the symbol extension: If the value is positive, insert 0 at the high level, or 1 in the high position if the value is negative. Java also adds a "unsigned" right shift operator (>>>), which uses "0 extensions": Inserts 0 at high levels, either positive or negative. This operator is not in C or C + +.

If Char,byte or short are shifted, they are automatically converted to an int before the shift occurs. Only 5 lows to the right are used. This prevents us from moving an unrealistic number of digits in an int. If a long value is processed, the resulting result is long. Only the 6 lows on the right are used at this point to prevent moving more than the number of bits in the Long value. However, you may also encounter a problem when you make an unsigned right shift. If you perform a right shift on a byte or a short value, you may not get the correct result (Java1.0 and Java1.1 are particularly prominent). They are automatically converted to type int and are shifted to the right. But "0 extensions" don't happen, so in those cases you get the result of 1.

Integer 4-byte 32-bit
Public byte[] Int2byte (int a)
{
byte[] bt = new BYTE[4];
Bt[0] = (byte) ((A & 0xff000000) >> 24);
BT[1] = (byte) ((A & 0x00ff0000) >> 16);
BT[2] = (byte) ((A & 0x0000ff00) >> 8);
BT[3] = (byte) (A & 0X000000FF);
Return BT;
}

Integer to byte array conversion

public static byte[] Int2bytes (int n) {

byte[] ab = new BYTE[4];

Ab[0] = (byte) (0xFF & N);

AB[1] = (byte) ((0XFF00 & N) >> 8);

AB[2] = (byte) ((0xff0000 & N) >> 16);

AB[3] = (byte) ((0xff000000 & N) >> 24);

return AB;

}

Conversion of byte array to integer

public static int Bytes2int (byte b[]) {

int s = 0;

s = (((b[0] & 0xff) << 8 | (B[1] & 0xff)) << 8) | (B[2] & 0xff)) << 8

| (B[3] & 0xff);

return s;

}

Private final static byte[] hex = "0123456789ABCDEF". GetBytes ();

private static int Parse (char c) {

if (c >= ' a ')

Return (C-' a ' +) & 0x0f;

if (c >= ' A ')

Return (C-' A ' +) & 0x0f;

Return (C-' 0 ') & 0x0f;

}

Convert from byte array to hexadecimal string

public static String bytes2hexstring (byte[] b) {

byte[] Buff = new byte[2 * B.length];

for (int i = 0; i < b.length; i++) {

Buff[2 * I] = hex[(B[i] >> 4) & 0x0f];

buff[2 * i + 1] = Hex[b[i] & 0x0f];

}

return new String (buff);

}

Convert from hexadecimal string to byte array

public static byte[] Hexstring2bytes (String hexstr) {

Byte[] B = new Byte[hexstr.length ()/2];

int j = 0;

for (int i = 0; i < b.length; i++) {

char C0 = Hexstr.charat (j + +);

Char C1 = Hexstr.charat (j + +);

B[i] = (byte) (Parse (C0) << 4) | Parse (c1));

}

return b;

}

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.