Hello friends,

I have a task where user will enter some number and he say to find out the first n bits from left side ( LBF) . now i need to find out the value of this first n bits in terms of hexadecimal notation.

One of my solution is like if user enter 2 bits to find out for a number like 23 then

0001 0111 is 23 i.e 17 and now 2 bits means 3 in hexa .

now if we and between 17 & 3 i will get 3 so this is the answer.

Here at runtime if user enters 5 bits and number is 56 (, then 1f should be AND with number 56 .)

so problem here is genearting the hexa decimal equalent.

one of the solution i think is first 5 bits means

here 1 = 1 , 2 bits '1' = 3 , 3 bits 1 = 7 ,4 bits 1 = f in hexa.

first step 5 bits so remove it from 4 ( as it is highest ) remaing 1 bit and 4 bits on are f

and 1 means 1 so 1f .

56 AND 1f = 18 ....( 18 in hex)

do we have any other logic or way (simpler than aboeve ) to generate the hexadecmial notation.

i tried to explain my problem , may be i should have written in more simpler manner.

thanks for your help guys