Finding a mask value with higher and lower position of the register

208 views Asked by At

A 32 bit register i.e. status_reg has 1 field (field_1 <0:31>). To set and clear this register I am trying to get mask value of this register. So in general to get mask value the formula is:

mask = ((~(~0 << (hbit-lbit+1))) << lbit)

In this hbit is higher position bit i.e. 31. And lbit is lower position bit i.e.0. I am expecting mask value should be 0xFFFFFFFF. But getting mask= 0. Can anyone suggest what is the wrong with mask?

I am using 32 bit system.

2

There are 2 answers

0
Malcolm McLean On

It shows undefined behaviour because you are left shifting outside of range, by 32 bits.

0
Holger On

I think your code doesn't give you the correct mask. If you like a mask for i.e. the bits 6..10 you can use (1<<(hbit-lbit+2))-1)<<lbit