The shortest solution of byte and int

Source: Internet
Author: User
1. Why is the byte range (-128 ~ (127 )?
-- Byte is an 8-bit
-- Two eight-bit representation formats are defined here: 1. the symbolic form (the first side is the symbol bit, which determines the positive and negative, and then calculates the remaining seven digits ). 2. Storage Format (memory is full-complement storage)
-- Want to know how to store a data in the memory? True Value> symbolic form> supplemental code> storage form
-- Example 1:
+ 8 (true value)
+ 8 (symbolic form): 0000 1000, // true value "+ 8", because it is "+", the first digit is defined as 0, because the value is "8 ", therefore, the remaining 7 digits are "000 1000 ".
+ 8 (Supplemental code): 0000 1000
+ 8 (Storage Format): 0000 1000, // because the positive complement is itself, the storage format in the memory is "0000 1000 ".


-- Example 2:
-8 (true value)
-8 (symbolic form): 1000 1000, // true value "-8", because it is "-", the first value is defined as 1, because the value is "8 ", therefore, the remaining 7 digits are "000 1000 ".
-8 (Anti-code): 1111 0111,
-8 (Supplemental code): 1111 1000,
-8 (Storage Format): 1111 1000, // after obtaining the complement code, the storage format of-8 in the memory is "1111 1000"


-- I already know the storage format of a number in the memory. How can I find this number? Storage Format => symbol form> supplemental code> symbol form> true value (Note: here is "=>", not "> ", indicates that "storage form" is used as "symbol form ")
-- Example 1:
+ 8 (Storage Format): 0000 1000
+ 8 (symbolic form): 0000 1000, // set "0000 1000" to the current symbolic form. The first digit is "0", indicating that this number is positive, so the complement code is itself
+ 8 (Supplemental code): 0000 1000,
+ 8 (symbolic form): 0000 1000, // the first digit is "0", then the true value is "+", and the last seven digits are "000 1000 ", the value is "8"
+ 8 (True Value): // so the true value is + 8


-- Example 2:
-8 (Storage Format): 1111 1000
-8 (symbolic form): 1111 1000, // set "1111 1000" to the current symbolic form. The first digit is "1", indicating that this number is a negative number and must be calculated as a supplementary code.
-8 (Anti-code): 1000 0111,
-8 (Supplemental code): 1000 1000,
-8 (symbolic form): 1000 1000, // the first digit is "1", then the true value is "-", and the last seven digits are "000 1000 ", the value is "8"
-8 (True Value): // the true value is-8.


-- Verification: (byte)-8 storage format is "1111 1000"
Int I =-8;
System. Out. println (integer. tobinarystring (I); // print the binary representation of the int type in the memory, "1111 1000"
System. out. println (byte) I); // print "-8". In this case, the byte-8 memory is represented as 24 bits on the left of the int type, that is, "1111 1000" is a byte-8 storage format, which is consistent with the results in the preceding example.




-- So what is the relationship between the above examples and the proof of byte range?
-- The byte value is 8 bits, so it must be "0000 0000" or "1111 1111". Because the first digit is the symbol bit, it can be divided into the following three parts.
-- Positive value range: 0000 0001 (representing + 1 )~~ 0111 1111 (more than 127)
-- 0 Storage Format: 0000 0000
-- Negative value range: 1000 0000 (representing-128 )~~ 1111 1111 (-1)
-- The above are all storage formats. The values of positive numbers and 0 are not explained. The range of negative numbers is explained as follows:
-- (1000 0000) solution process:
(Storage Format): 1000 0000
(Symbolic form): 1000 0000, // the first digit is "1", indicating that this number is a negative number and it needs to be calculated as a complement.
(Reverse code): 1111 1111,
(Complement): 1000 0000, // the first digit is "1", then the true value is "-", and the last seven digits are "111 1111 ", the value is "1000 0000", and the value is "128"
(Symbolic form): 1000 0000, // the true value is "-128 ",


-- (1111 1111) solution process:
(Storage Format): 1111 1111
(Symbolic form): 1111 1111, // the first digit is "1", indicating that this number is a negative number and it needs to be calculated as a complement.
(Reverse code): 1000 0000,
(Supplemental code): 1000 0001, // the first digit is "1", then the true value is "-", and the last seven digits are "000 0000 ", the supplementary code is "000 0001", and the value is "1"
(Symbolic form): 1000 0000, // the true value is "-1 ",




2. int> byte
-- (1). Suppose the int value range is (-128 ~ 127 ):
-- Example 1:
Int I = 64;
System. Out. println (integer. tobinarystring (I); // Storage Format: 0000 bytes, 0000 bytes, bytes
System. Out. println (byte) I); // remove the 24 bits on the left and the remaining "0100 0000". This is the storage format. Print 64


-- Example 2:
Int I =-64;
System. Out. println (integer. tobinarystring (I); // Storage Format: 1111 1111,111111,111111,1100 0000
System. Out. println (byte) I); // remove the 24 bits on the left and the remaining "1100 0000". This is the storage format. Print-64 using the Reverse Method in the preceding example



-- (2). Suppose the int value range is (> 127, <-128 ):
-- Example 1:
Int I = 128;
System. Out. println (integer. tobinarystring (I); // Storage Format: 0000 bytes, 0000 bytes
System. Out. println (byte) I); // remove the 24 bits on the left and the remaining "1000 0000". This is the storage format. Print-128 Using the Reverse Method in the preceding example.


-- Example 1:
Int I =-129;
System. Out. println (integer. tobinarystring (I); // Storage Format: 1111 1111,111111,11111111,0111 1111
System. Out. println (byte) I); // remove the 24 bits on the left and the remaining "0111 1111". This is the storage format. Print 127 by reversing the method in the preceding example.




3. byte> INT: Why is the value after the 8-bit byte type-8 is converted to the 32-bit int type?
-- Example 1:
Byte B = 8; // byte Storage Format: 0000 1000
System. Out. println (INT) B); // because "8" is a positive number, add 24 digits "0" on the left"
// Int Storage Format: 0000 seconds, 1000 seconds, through the Reverse Method in the above example, so the int value is also 8


-- Example 2:
Byte B =-8; // byte storage: 1111 1000
System. Out. println (INT) B); // because "8" is a negative number, add 24 "1" to the left"
// Int Storage Format: 1111 111111,111111,11111111,1111 1000. Through the Reverse Method in the above example, the int value is also-8


4. Recently, we have seen a byte-to-int method "Byte & 255" or "Byte & 0xff". The following describes how to parse the process conversion.
Int getint (){
Byte B =-8;
Return B & 255;
}


-- Parsing process:
Byte B =-8; // byte storage: 1111 1000
// The Int storage format is 1111 1111,111111,1111 1111,1111 1000
// 255 int Storage Format: 0000 bytes, 1111 bytes, bytes
// Perform & Operation: 0000 seconds, 1000 seconds, 248 seconds, and the printed result is.
// Int to byte type, remove the 24 digits on the left: 1111 1000, print-8 using the Reverse Method in the above example, so there is no conflict, here, we will only explain the "& 255" operation process.

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.