Thread: hexadecimal bit operation

  1. #1
    Registered User
    Join Date
    Mar 2008
    Location
    India
    Posts
    147

    hexadecimal bit operation

    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

  2. #2
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    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
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  3. #3
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    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.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. 32 bit to 64 bit Ubuntu
    By Akkernight in forum Tech Board
    Replies: 15
    Last Post: 11-17-2008, 03:14 AM
  2. bit value check efficiency
    By George2 in forum C Programming
    Replies: 5
    Last Post: 11-05-2007, 07:59 AM
  3. Replies: 7
    Last Post: 12-10-2004, 08:18 AM
  4. bit patterns of negtive numbers?
    By chunlee in forum C Programming
    Replies: 4
    Last Post: 11-08-2004, 08:20 AM
  5. Array of boolean
    By DMaxJ in forum C++ Programming
    Replies: 11
    Last Post: 10-25-2001, 11:45 PM