Negative binary positive number source code anti-Code complement

Source: Internet
Author: User
    • A negative number of binary values.

Hex (hexadecimal) EA

Oct (decimal) signed and unsigned

-22 234

Bin (Binary) 1110 1010 11101010

 

Note: Number of symbols

Positive source code, reverse code, and complement code are consistent

For details about negative numbers, see 11101010.

1 1 1 0 1 0 1 0
symbol bit (not involved in reverse and + 1 behavior)
1) reverse 1 0 0 1 0 1 0 1 get the reverse Code
2) + 1 1 1 1 0 1 0 1 0 get the supplemental Code
1 (representing a negative number)

-22

 

    • Conversion between Java Byte and hexadecimal strings

Hex string --> byte

 
//1 hex string --> integer//2 integer --> bytePublicByte convertfromhexstring2byte (string hexstr) {INTEGER I= Integer. parseint (hexstr, 16); Byte B=I. bytevalue ();}

Byte --> hex string

//1 byte --> integer//2 integer --> stringPublic StaticString frombyte2hexstring (byte B) {INTEGER I=B. intvalue (); string s=Integer. tohexstring (I );ReturnS ;}

 

A byte occupies 8 characters, and a hex character is represented by 4 characters ==> 1 byte represents 2 hex characters ==>

[] [] [] [] [] [] [] [] []

High 4-bit low 4-bit

H L

 

    • Value range of byte type (byte occupies 8 bits, with the symbol-128 --- 127)

Reasoning

                  Description  
Max positive number 0 1 1 1 1 1 1 1 ===> 127

20 + 21 + .... + 28-1 = 28-1 = 127

Maximum negative number 1 0 0 0 0 0 0 0 ==>> 128

-27

  1 1 1 1 1 1 1 1 Reverse code  
1 1 1 1 1 1 1 1 1 Complement  
                     

Value Range

Byte 1 byte 8bit -2 (7) --- 2 (8)-1 Signed

-2n-1 --- 2n-1-1

Short 2 bytes 16bit

-215-215-1

 

Same as above

Int 4 bytes 32bit    

Same as above

 

Long 8 bytes 64bit    

Same as above

 

Char 2 bytes 16bit

0--216-1

Unsigned

0-2n-1

 

    • Byte = "hex string Conversion
 Public  Static String byte2hexstring ( Byte  [] B) {string ref = "" ;  For ( Int I = 0; I <B. length; I ++ ){/*
Integer. tohexstring (B [I] & 0xff);
The function is int (decimal signed) ==> string (hexadecimal)
B [I] & 0xff converts byte to int type
Byte 8-bit int 32-bit byte to int-bit is not enough to make up the bit offset, so it is necessary to clear the high 24-bit, that is, & 0xff */
String hex
= Integer. tohexstring (B [I] & 0xff ); If (Hex. Length () = 1 ) {Hex = '0' + Hex;} ref + = Hex. touppercase ();} Return Ref ;} /* FF hexadecimal 1111 1111 Binary-1 signed decimal 255 unsigned */
    • Relationship

Both sides can be true

1 & 1 -- "1

1 & 0 -- "0

0 & 1 -- "0

0 & 0 -- "0

Clear = "& 0

Set to 1 = "& 1

Byte B = 24;

Int I = B & 0xff; // clear 24-bit high

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.