-2

How to iterate through a binary number in c++?

In the mentioned function I'm getting

invalid operands to binary conversion (bitset<8> and int)

This is the function in my code, which is getting the stated error

int value(int x)
{
    int temp=0,counter=0;
    bitset<8> i(x);
    while (i != 0) {
       temp = i % 10;
       if(temp == 1) {
           counter++;
       }
       i = i/10;
  }
  return counter;
}
The Apache
  • 1,076
  • 11
  • 28
  • X is the number whose binary value i need, and then from that binary number I'm supposed to find the number of one's that number has. – Sanjay Jain Sep 24 '16 at 17:19
  • 1
    You're not using the bitset here really. Your code is attempting to iterate over the number in decimal not binary. – iksemyonov Sep 24 '16 at 17:19
  • 1
    If you want to count bits, you have [`bitset::count()`](http://en.cppreference.com/w/cpp/utility/bitset/count). – Bo Persson Sep 24 '16 at 17:19
  • i want to count the number of one's in that binary number. so i thought of using that method. – Sanjay Jain Sep 24 '16 at 17:20
  • 1
    Look at [how-to-count-the-number-of-set-bits-in-a-32-bit-integer](http://stackoverflow.com/questions/109023/how-to-count-the-number-of-set-bits-in-a-32-bit-integer) – Jarod42 Sep 24 '16 at 17:25
  • @WhozCraig . okay but can you please tell me how do i specially calculate the number of 1's in the binary number – Sanjay Jain Sep 24 '16 at 17:25
  • @WhozCraig that needs c++11 http://stackoverflow.com/questions/31632491/constexpr-for-sizeof-in-template-does-not-compile – iksemyonov Sep 24 '16 at 17:26
  • @iksemyonov: `sizeof...` != `sizeof`. – Jarod42 Sep 24 '16 at 17:29
  • @Jarod42 sorry, indeed, didn't notice. But isn't the usual `sizeof` `constexpr` as well? I mean, how does it work in templates then. – iksemyonov Sep 24 '16 at 17:30
  • 4
    @WhozCraig: `sizeof(x)` gives the number of chars in x, not the number of bits. – Daniel Sep 24 '16 at 17:30
  • @Dani let me uptick that for you, twice if I could =P (no coffee this morning). Post the correct answer and I'll uptick that 4u too. – WhozCraig Sep 24 '16 at 17:31

1 Answers1

3

To count the number of 1's in the first 8 bits of x:

int value(int x)
{
  return bitset<8>(x).count();
}

To count all the bits:

int value(int x)
{
  return bitset<sizeof(x) * CHAR_BIT>(x).count();
}

If you have to use a loop solution: (Adaption to your solution with available functions)

int value(int x)
{
    int counter=0;
    bitset<8> i(x);
    while (i != 0) {
       if(i[0] == 1) {
           counter++;
       }
       i >>= 1;
  }
  return counter;
}
Daniel
  • 30,896
  • 18
  • 85
  • 139
  • @Jarod42: As long as the conversion doesn't overflow there is nothing implementation specific. You are correct, it is better, but in answers I prefer to stick with the OP's signatures. – Daniel Sep 24 '16 at 17:51
  • hey @Dani , i implemented your code in my program, the only problem is for all the numbers with one 1's in their binary numbers such as 2, 8 I'm getting answer with junk values such as 14343434. – Sanjay Jain Sep 24 '16 at 17:53
  • @SanjayJain: I tested all variants and they all worked. Can you show your whole code? – Daniel Sep 24 '16 at 17:57
  • yep @dani here it is . #include #include using namespace std; int value(int i); int main() { int t; cin>>t; while(t!=0) {int n=0; cin>>n; int i=-1; int arr[n]; for(int i=0;i>arr[i]; } – Sanjay Jain Sep 24 '16 at 18:01
  • for(int j=0;j value(arr[x])) { int temp; temp = arr[x]; // swap elements arr[x] = arr[x+1]; arr[x+1] = temp; } else if (value(arr[x+1])==value(arr[x])) { continue; } } } cout< – Sanjay Jain Sep 24 '16 at 18:01
  • int value(int x) { int counter=0; bitset<8> i(x); while (i != 0) { if(i[0] == 1) { counter++; } i >>= 1; } return counter; } – Sanjay Jain Sep 24 '16 at 18:01
  • @SanjayJain: 1. This is hard to read, a pastebin would be better. 2. You need to isolate your points of failure, did you try checking what input `value` is getting via a debugger/print? 3. In a quick look I can spot a bug: in the sort(?) loop when `x = n-1`, `arr[x+1]` is out of range. – Daniel Sep 24 '16 at 18:05
  • the out of range correction worked for me thanks a lot – Sanjay Jain Sep 24 '16 at 18:09