Decimal number to binary representation

Source: Internet
Author: User
Tags bitset

I wrote an article describing how to use bitset to express the actual binary layout of int variables stored in the computer, which is very useful in my work.

This article uses algorithms to convert a 10-digit integer to a binary number.

Logically, if the decimal number 5 is represented in one byte, it should be 101. You can add 1 continuously through 000 to get this number. However, division is generally used.

5/2

The quotient is 2 and the remainder is 1.

Users: 2/2,

The result is that the quotient is 1 and the remainder is 0.

Service provider 1/2

The result is that the quotient is 0 and the remainder is 1.

In reverse order, the remainder (the final remainder is placed at a high level) is 101.

This algorithm can be implemented using recursion:

// Convert decimal integer in one byte to binary format stringstring ByteToBinaryString(char v) {    if (v == 1) {        return "1";     }        if (v % 2 == 0) {        return ByteToBinaryString(v / 2) + "0";    } else {        return ByteToBinaryString(v / 2) + "1";    }}

Bytetobinalystring converts a 10-digit integer stored in a byte to a binary string.

Every time bytetobinalystring is called, the quotient and remainder divided by 2 are calculated first, and the consumer continues to call himself recursively. After the remainder is connected to the return value of the next recursive function, a reverse order is achieved.

When the outlet provider is 1, "1" is returned directly when 1 is used to call itself ".

After testing, there is basically no problem in positive numbers. If it is 0, an error occurs. Because recursion becomes infinite, there is no exit. So we need to add some code to ensure that the program can work normally when 0 is directly passed as the parameter.

// Convert decimal integer in one byte to binary format stringstring ByteToBinaryString(char v) {    if (v == 1) {        return "1";     }        if (v == 0) {        return "0";    }        if (v % 2 == 0) {        return ByteToBinaryString(v / 2) + "0";    } else {        return ByteToBinaryString(v / 2) + "1";    }}

The Char value is smaller than 0.

-1 according to the previous article: http://blog.csdn.net/sheismylife/article/details/7626219 description, should be 11111111. Because 1 is 00000001, take the inverse and Add 1.

Obviously, this program cannot meet this requirement. Put it here first.

Below is a simpler method to print out the system memory representation directly. Use bitwise AND to collide.

// Get the bit value specified by the index// index starts with 0template<class T>int Bit_Value(T value, uint8_t index) {    return (value & (1 << index)) == 0 ? 0 : 1;}// T must be one of integer typetemplate<class T>string PrintIntAsBinaryString(T v) {    stringstream stream;    int i = sizeof(T) * 8 - 1;    while (i >= 0) {        stream << Bit_Value(v, i);        --i;    }        return stream.str();}

Now check the value of-7:

    int8_t f = -7;    string h = PrintIntAsBinaryString<int8_t>(f);    cout << "h:" << h << endl;

h:11111001

The value of 7 is printed as follows:

h:00000111

Now this function can replace the bitset method: http://blog.csdn.net/sheismylife/article/details/7625526

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.