Print the binary representation of an integer (including a negative number) inside the computer

Source: Internet
Author: User

Print the binary representation of an integer (including a negative number) inside the computer

Before understanding the binary representation of integers, we must first understand the following three concepts: source code, complement code, and reverse code.Source code isThe binary representation of an integer. The highest digit is the sign bit. We use 0 to represent a positive number and 1 to represent a negative number. If an integer is expressed in 8 bits, the source code of 5 can be expressed as 00000101. The source code of-6 is 10000110.The anti-code isThe source code symbol bit (that is, the highest bit) remains unchanged, and the other bit gets the reversed binary code.Complement isAdd the bitwise of the anticode to the binary number 1 to obtain the binary code.In computer systemsIn, all values are represented and stored by the complement code.The reason is that the signed and numeric bits can be processed in a unified manner using the complement code. addition and subtraction can also be processed in a unified manner. In addition, when the complement code and the original code are converted to each other, the calculation process is the same, and no additional hardware circuit is required.

Positive complement: same as the original code. Negative complement: The symbol does not move, and the anti-code is added to 1.

Take the following example:

<喎?http: www.bkjia.com kf ware vc " target="_blank" class="keylink"> VcD4KPHA + ztLDx7/J0tTTw8jnz8K0 + fill + a1xl3hwtuju.pdf vcD4KPHA + fill = "brush: java;"> package test;/*** four integers in Java, that is, 32-bit * @ author wl **/public class PrintIntByBinary {public static void main (String [] args) {int a1 = 3; System. out. print (a1 + "binary representation:"); printBinaryOfInt (a1); int a2 =-3; System. out. print (a2 + "binary representation:"); printBinaryOfInt (a2);} public static void printBinaryOfInt (int num) {int t = 0; for (int I = 0; I <32; I ++) {// loop 32 times, it should be 32-bit t = (num & 0x80000000 >>> I) >>> (31-i) in Java ); /* 0x80000000 indicates that the highest bit is 1 and the other 31 bit is 0. >>> indicates the unsigned right shift. When I = 0, 0x80000000> 0: The result is still 0x80000000. num & 0x80000000 indicates that the highest bit of num is obtained, and all other bits are set to 0, then perform The >>> (31-i) operation on the result, that is, move the result unsigned 31 times to the right, and obtain the highest bit. When I = 1, 2, 3 ..... in sequence, the value of the 29th-bit until the second-bit is retrieved */System. out. print (t);} System. out. println ();}}

Output:

The binary representation of 3 is: 00000000000000000000000000000011

-3 binary representation: 11111111111111111111111111111101




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.