Search code examples
cbit

Zero all the bits after the 2nd index


i want to zero all the bits after the 2nd index (Including the 2nd) in an unsigned int. Here's the non working code i wrote so far: (temp is an unsigned int.)

for(int i=2; i< DSLength(dnaS); i++)
        {
            temp = temp & (0 << i);
        }

It keeps zeroing the whole number...


Solution

  • If I understand you correct, and you want to preserve the lowest two bits and zero all the rest, you don't need a loop:

    x &= 3
    

    does exactly that with x.