-1
#include <iostream>
using namespace std;
void binary(unsigned  a) {
    int i;
    cout << "0" << endl;
    do {
        for (i = 1; i < a; i=i/2) {
            if ((a & i) != 0) {
                cout << "1" << endl;
            }
            else {
                cout << "0" << endl;
            
            }
    
        }
    }
    while (1 <= a && a <= 10);
}

int main(void) {
    binary(4);
    cout << endl;
}

I wrote a code about binary numbers. İt should give bits respect to entering number like for 4 (0100) for 2 (10). However my code goes infinity could you explain. I wrote in visual studio and I cannot use <bits/stdc++.h> because there is no such a library in visual studio

Sude Şahin
  • 11
  • 1
  • 4
  • 1
    ***I wrote in visual studio and I cannot use because there is no such a library in visual studio*** That is a very good thing. Never use this header ever. Related: [https://stackoverflow.com/Questions/31816095/Why-Should-I-Not-Include-Bits-Stdc-H.](https://stackoverflow.com/Questions/31816095/Why-Should-I-Not-Include-Bits-Stdc-H.) – drescherjm Oct 11 '21 at 12:46
  • Ask yourself: If `a` is `4`, when does `while (1 <= a && a <= 10)` end? – NathanOliver Oct 11 '21 at 12:47
  • @NathanOliver it can change I just wrote . İt should be in function and our teacher can give another number too but according to constraints it should be between 1 and 10 – Sude Şahin Oct 11 '21 at 12:51
  • 1
    *because there is no such a library in visual studio* -- Hurray for Visual Studio. -- *and our teacher* -- Your teacher is giving you `#include `? I hope not. – PaulMcKenzie Oct 11 '21 at 12:52
  • You should search the internet for "c++ binary number cout" or something similar. There are a lot of similar questions on both StackOverflow and the internet. – Thomas Matthews Oct 11 '21 at 14:48
  • In your loop, `i` has an initial value of `1` (actually `1u`) and the loop keeps dividing `i` by `2` (via `i = i/2`). Dividing `1` by `2` produces `0` (rounding toward zero). Dividing `0` by `2` also produces `0`. So the only possible values of `i` in that loop are `1` and `0`. If `a` has a value of `4`, the test `i < a` will always be true. Hence an infinite loop. – Peter Oct 11 '21 at 16:12

2 Answers2

2

Initially i is 1 but i = i / 2 sets i to 0, where it remains. The inner loop, therfore, loops for ever.

To output an unsigned number a in binary, use

#include <bitset>
#include <climits>
std::cout << std::bitset<sizeof(a) * CHAR_BIT>(a) << '\n';

(There is, at the time of writing no std::bin i/o manipulator cf. std::hex.)

Bathsheba
  • 231,907
  • 34
  • 361
  • 483
1

Without using a built-in function, you can write your own function and perform your operation as follows.

Solution-1

#include <iostream>
void binary(unsigned int number)
{
    if (number / 2 != 0) {
        binary(number / 2);
    }
    std::cout << number % 2;
}

int main() {
    binary(10);
}

Solution-2

#include <iostream>
#include<string>

void binary(unsigned int number)
{
    std::string str = "";
    while (number != 0) { 
        str = (number % 2 == 0 ? "0" : "1") + str;
        number /= 2; 
    }
    std::cout << str;
}
int main()
{
   binary(4);
}

Note : Don't use using namespace std; . Why is "using namespace std;" considered bad practice?

east1000
  • 1,240
  • 1
  • 10
  • 30