Convert to binary in C ++

I created a function that converts numbers to binary. For some reason this does not work. This gives the wrong result. The result is in binary format, but it always gives the wrong result for binary numbers ending in zero (at least what I noticed ..)

unsigned long long to_binary(unsigned long long x)
{
    int rem;
    unsigned long long converted = 0;

    while (x > 1)
    {
        rem = x % 2;
        x /= 2;
        converted += rem;
        converted *= 10;
    }

    converted += x;

    return converted;
}

Please help me fix this, it is really frustrating.

Thank!

+3
source share
9 answers
  • You change the bits.
  • You cannot use leftover x as an indicator to complete the loop.

Consider, for example, 4.

After the first iteration of the loop:

rem == 0
converted == 0
x == 2

After the second iteration of the loop:

rem == 0
converted == 0
x == 1

And then you set the conversion to 1.

Try:

int i = sizeof(x) * 8; // i is now number of bits in x
while (i>0) {
  --i;
  converted *= 10;
  converted |= (x >> i) & 1;
  // Shift x right to get bit number i in the rightmost position, 
  // then and with 1 to remove any bits left of bit number i,
  // and finally or it into the rightmost position in converted
}

x unsigned char (8 ) 129 ( 10000001)

i = 8, unsigned char * 8. i 7. x (129) 7 , 1. OR'ed converted, 1. converted 10 ( 10), x 6 ( 2) AND 1 ( 0). 0 converted, 10. , converted 10, x OR'ed converted. converted 1000000.

converted 10 10000000, x right 0 bits, 129. x 1, 1. 1 OR'ed converted, 10000001.

+2

std:: bitset :

#include <iostream>
#include <bitset>
#include <limits.h>

int main()
{
    int     val;
    std::cin >> val;

    std::bitset<sizeof(int) * CHAR_BIT>    bits(val);
    std::cout << bits << "\n";

}
+3

;)

http://www.bellaonline.com/articles/art31011.asp

, .

- :

unsigned long long to_binary(unsigned long long x)
{
    int rem;
    unsigned long long converted = 0;
    unsigned long long multiplicator = 1;

    while (x > 0)
    {
        rem = x % 2;
        x /= 2;
        converted += rem * multiplicator;
        multiplicator *= 10;
    }

    return converted;
}

edit: , CygnusX1, , , , .

: while, , x .

+1

! to_binary (2) 01, 10. 0es , , 1.

:

unsigned long long digit = 1;
while (x>0) {
  if (x%2)
    converted+=digit;
  x/=2;
  digit*=10;
}
0

, . , , STL-.

#include <bitset>
#include <iostream>
#include <sstream>

typedef std::bitset<64> bitset64;


std::string to_binary(const unsigned long long int& n)
{
        const static int mask = 0xffffffff;
        int upper = (n >> 32) & mask;
        int lower = n & mask;
        bitset64 upper_bs(upper);
        bitset64 lower_bs(lower);
        bitset64 result = (upper_bs << 32) | lower_bs;
        std::stringstream ss;
        ss << result;
        return ss.str();
};

int main()
{
        for(int i = 0; i < 10; ++i)
        {
                std::cout << i << ": " << to_binary(i) << "\n";
        };
        return 1;
};

:

0: 0000000000000000000000000000000000000000000000000000000000000000
1: 0000000000000000000000000000000000000000000000000000000000000001
2: 0000000000000000000000000000000000000000000000000000000000000010
3: 0000000000000000000000000000000000000000000000000000000000000011
4: 0000000000000000000000000000000000000000000000000000000000000100
5: 0000000000000000000000000000000000000000000000000000000000000101
6: 0000000000000000000000000000000000000000000000000000000000000110
7: 0000000000000000000000000000000000000000000000000000000000000111
8: 0000000000000000000000000000000000000000000000000000000000001000
9: 0000000000000000000000000000000000000000000000000000000000001001
0

, itoa std::bitset

#include <stdlib.h>
#include <stdio.h>
#include <iostream>
#include <bitset>

using namespace std;

int main()
{
    unsigned long long x = 1234567890;

    // c way
    char buffer[sizeof(x) * 8];
    itoa (x, buffer, 2);
    printf ("binary: %s\n",buffer);

    // c++ way
    cout << bitset<numeric_limits<unsigned long long>::digits>(x) << endl;

    return EXIT_SUCCESS;
}
0
void To(long long num,char *buff,int base)
{
    if(buff==NULL)      return;
    long long m=0,no=num,i=1;

    while((no/=base)>0) i++;
    buff[i]='\0';

    no=num;
    while(no>0)
    {
        m=no%base;
        no=no/base;
        buff[--i]=(m>9)?((base==16)?('A' + m - 10):m):m+48;
    }
}
0

.

#include <iostream>
using namespace std;
int main()
{
    int num=241; //Assuming 16 bit integer
    for(int i=15; i>=0; i--) cout<<((num >> i) & 1);
    cout<<endl;
    for(int i=0; i<16; i++) cout<<((num >> i) & 1);
    cout<<endl;
    return 0;
}
0

Source: https://habr.com/ru/post/1795257/


All Articles