lundi 4 mars 2019

Obtaining range of bits its from a given no

I am using the following function to extract n bits from a number. I got this function from here. For convenience I am posting it here. I would like to obtain bits from 0 to 9 and then in another statement bits 10 to 15. I am passing in 1033. I get the correct value of bits 0 to 9 but incorrect value for 10 to 15. I should get a 1 instead i am getting 1024 any suggestions ?

 unsigned createMask(unsigned a, unsigned b)
    {
        unsigned r = 0;
        for (unsigned i = a; i <= b; i++)
            r |= 1 << i;

        return r;
    }

Now i have this

unsigned short langId = 1033 ;// 10000001001
unsigned primary =   createMask(0,9) & langId; //gives 9 correct
unsigned sec = createMask(10,15) & langId;     //gives 1024 incorrect should be 1

Aucun commentaire:

Enregistrer un commentaire