"C + +" gets the binary form string of integers

Source: Internet
Author: User

Ideas


Use a loop to move an integer to the left I bit, and take the highest bit, if the current maximum is 1 then the value is ' 1 ', otherwise ' 0 '. This stitching out a string.


Note: The digital complement is stored in the computer.


About positive, negative number of the original code, anti-code, and complement.

Positive number: The original code, anti-code, and complement are the same.
Negative number: Anti-code (except for the highest bit, other bits to reverse) complement (anti-code + 1)


Why should I store the complement instead of the original code?

In order to unify the operation method. Use the complement to add and subtract signed integers without needing to care about the sign bit.
Example: 1 + (-2) = 1
-2 of the original code
1000 0000 0000 0000 0000 0000 0000 0010
-2 of the anti-code
1111 1111 1111 1111 1111 1111 1111 1101
-2 of the complement
1111 1111 1111 1111 1111 1111 1111 1110

Calculation process:
0000 0000 0000 0000 0000 0000 0000 0001
+ 1111 1111 1111 1111 1111 1111 1111 1110
= 1111 1111 1111 1111 1111 1111 1111 1111
-1 Complement: 1111 1111 1111 1111 1111 1111 1111 1111


The use of complement simplifies the calculation of signed integers into one step, which greatly improves efficiency.
If using the original code: 1. Determine the sign bit of the number. 2. Take the absolute value of the addition/meiosis 3. Plus/minus absolute value

Code




"C + +" gets the binary form string of integers

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.