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.

2. Not sure what you are asking for, but basically, if you want to know what the first n bits are in hex, you do:
Code:
```   while (n >= 4)
hexnumber = 'F' + hexnumber;
n -= 4;
switch(n)
{
case 1:
hexnumber = '1' + hexnumber;
break;
case 2:
hexnumber = '3' + hexnumber;
break;
case 3:
hexnumber = '7' + hexnumber;
break;
}```

Of course, in C you can make a n-bit mask with:
Code:
`   mask = (1 << n) - 1;`
--
Mats

3. Why do you need to know what it looks like in hex? As Mats says, you just make n bits by putting in a bunch of 1s. If you need to print it, then print it with %x and be done with it.