Question

I was coding and the following code doesn't give the desired output. pos&1 is supposed to return the remainder when pos is divided by 2. When I replace pos&1 by pos%2 everything works just fine. What could be the problem?

#include <iostream>
using namespace std;
int main(){
    int y;
    unsigned long long int pos;
    cin>>y;
    cin>>pos;
    int f=0;
    while(y>0){
        y--;
        if(pos&1==0){
            f=1-f;
        }
        pos=pos/2;
    }
    if(f==1){
        cout<<"blue\n";
    }
    else
    cout<<"red\n";
    return 0;
}
Was it helpful?

Solution

1==0 takes more precedence than pos&1. Try if((pos&1)==0){

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top