-1

This is what I have for my conversion function so far It takes an unsigned integer as a parameter. It should give the result like

outputBinary(1) //=> 0000 0000 0000 0000 0000 0000 0000 0001
outputBinary(5) //=> 0000 0000 0000 0000 0000 0000 0000 0101
outputBinary(1000000) //=> 0000 0000 0000 1111 0100 0010 0100 0000
void outputBinary(unsigned int x){
  int temp = x;
  int remain;
  string binary = "";
  while(temp != 0){
    remain = temp%2;
    binary = binary + to_string(remain);
    temp = temp/2;
  }
  cout << binary << endl;
}
Hongyan Wu
  • 11
  • 4
  • 3
    And your issue is? – JHBonarius Sep 25 '20 at 08:51
  • Welcome to Stack Overflow. Please read [the help pages](http://stackoverflow.com/help), take the SO [tour], read [ask], as well as [this question checklist](https://codeblog.jonskeet.uk/2012/11/24/stack-overflow-question-checklist/). Lastly please learn how to [edit] your questions to improve them, for example to actually ask us a question. – Some programmer dude Sep 25 '20 at 08:56
  • My crystal ball tells me your problem is this outputs the bits in reverse order. – WhozCraig Sep 25 '20 at 08:57
  • I'm wondering how to make this into a 32 bits binary representation, which takes account the zeros before it. – Hongyan Wu Sep 25 '20 at 09:23
  • Hello, please write in your question what is the output you get from your code, and what is the output you want. It will help! – Olivier Sohn Sep 25 '20 at 09:30
  • If you need 32 digits padded with zero, use a `for` loop to count to 32 instead of a while loop that stops when you're out of ones. If you want the digits in the correct order, flip the two sides of `+` on the line with `binary + to_string(remain)`. – parktomatomi Sep 25 '20 at 09:33
  • When you have `std::bitset`, don't reinvent the wheel unless it's assignment. Ref: https://stackoverflow.com/questions/7349689/how-to-print-using-cout-a-number-in-binary-form – Louis Go Sep 26 '20 at 00:49

2 Answers2

0

First of all, if you want 32 bit representation, use uint32_t as type input for your function.

Secondly, instead of:

while(temp != 0){
    remain = temp%2;
    binary = binary + to_string(remain);
    temp = temp/2;
}

write something like

for(int i=0;i<32;++i){
    remain = temp%2;
    binary = to_string(remain).append(binary);
    temp = temp/2;
}

Of course this is not optimal. Instead i would suggest using something like bit shift operators and a char array with fixed size in which you just replace the character with 0 or 1 in the loop.

Arron Stowne
  • 111
  • 3
  • Thank you. And may I asked what does append do? – Hongyan Wu Sep 26 '20 at 03:42
  • Well, initially binary is an empty string variable. At each step here, it is replaced by combination of new bit as a string and previous value. For example for 8 in binary, the values will change this way: 0, 0+0, 0+00, 1+000,0+1000,0+01000 and so on, where + is basically the apend operathor and the right part is the value from previous iteration of the string – Arron Stowne Sep 26 '20 at 15:05
0

If you want to print all the "leading" zero, you can't use while(temp != 0){. You'll have to make sure to loop exactly 32 times. A for loop can be used for this.

Further, use uint32_t to ensure that the input has 32 bits.

Like:

void outputBinary(uint32_t x){
  std::string binary = "";
  for(int i = 0; i < 32; ++i){
    if (i % 4 == 0) binary = " " + binary;
    binary = std::to_string(x % 2) + binary;
    x = x/2;
  }
  std::cout << binary << std::endl;
}

If called like outputBinary(1000000); the output is:

0000 0000 0000 1111 0100 0010 0100 0000
Support Ukraine
  • 42,271
  • 4
  • 38
  • 63