vendredi 7 août 2020

What n&1 and n>>=1 mean? [duplicate]

I was doing this exercise : https://www.hackerrank.com/challenges/30-binary-numbers/problem and I found this "smart" code on the net, but I didn't understand what the condition with n&1 and n>>=1 do here.

 //C++ program to convert a decimal
// number to binary number

#include <iostream>
using namespace std;

int main()
{
    int n,count=0,max=0;
    cin >> n;

    while(n)
    {
        if (n&1)
            count++;
        else
            count = 0;
        if (max < count)
            max = count;
        n>>=1;
    }
    cout << max;

    return 0;
}

Aucun commentaire:

Enregistrer un commentaire