Thread: Hexadecimal value

  1. #1
    Registered User
    Join Date
    Apr 2008
    Posts
    15

    Hexadecimal value

    I have a variable that is initialized to a hexadecimal value. What I am trying to do is break the value into several parts. So pretty much my variable is set to something like 0x22510064 which is 0010 0010 0101 0001 0000 0000 0110 0100 in binary and I want to copy the first six bits into another variable(001000). I am not sure if I have to write code that will convert the hex into binary or if I could I just leave it in the hex format.

    What I was thinking about doing accomplish the partition was to copy the entire binary to the other variables and then deleting the parts I don't want.

  2. #2
    Registered User
    Join Date
    Oct 2008
    Location
    TX
    Posts
    2,059
    Do you just want to store those 6 bits in a char array and it would have been far easier if you had wanted multiples of a nybble.

  3. #3
    Registered User
    Join Date
    Apr 2008
    Posts
    15
    Quote Originally Posted by itCbitC View Post
    Do you just want to store those 6 bits in a char array and it would have been far easier if you had wanted multiples of a nybble.
    Well not an array because my professor gave us a code he wrote already and the variables we are to store these bits in are just unsigned. This is the first time I have ever seen the unsigned modifier, but I read that it just removes the signed bit so that the value you can store doesn't have to worry about being negative, for an int at least. What I am attempting to do is create a simple processor in C. I understand every other function I am writing so far but this one is essential since the hexadecimal represents a certain instruction which I later have to decode.

    I am not sure what a nybble is, but that the hex format is the one he said our array that holds the instructions should be in.
    Last edited by MasterAchilles; 11-18-2008 at 10:16 PM.

  4. #4
    Registered User
    Join Date
    Oct 2008
    Location
    TX
    Posts
    2,059
    Quote Originally Posted by MasterAchilles View Post
    Well not an array because my professor gave us a code he wrote already and the variables we are to store these bits in are just unsigned. This is the first time I have ever seen the unsigned modifier, but I read that it just removes the signed bit so that the value you can store doesn't have to worry about being negative, for an int at least. What I am attempting to do is create a simple processor in C. I understand every other function I am writing so far but this one is essential since the hexadecimal represents a certain instruction which I later have to decode.
    if you have the code why not use it or atleast post it in these forums and someone will help you out.
    Quote Originally Posted by MasterAchilles View Post
    I am not sure what a nybble is, but that the hex format is the one he said our array that holds the instructions should be in.
    you can always Google "nybble" which is 4 bits; one hex digit; half-byte.

  5. #5
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    This sounds like a job for bitwise operations. If you have a 32-bit integer, which it seems that you do, then variable >> 26 would shift the 26 least significant bits out of the number, leaving you with the six most significant bits, and you're done. (And unsigned is important here, as it will make sure that the bit shift will fill in with zeroes.)

  6. #6
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Quote Originally Posted by tabstop View Post
    (And unsigned is important here, as it will make sure that the bit shift will fill in with zeroes.)
    However, we can ensure that the upper bits are zero by doing an and (&) with the bits we want.
    Code:
    x &= ((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.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 7
    Last Post: 12-02-2007, 05:55 AM
  2. hexadecimal to ascii
    By jpablo in forum C Programming
    Replies: 2
    Last Post: 04-12-2006, 05:28 PM
  3. would you explain to me? hexadecimal to decimal
    By shteo83 in forum C Programming
    Replies: 2
    Last Post: 02-25-2006, 03:55 PM
  4. hexadecimal
    By pierremk in forum C++ Programming
    Replies: 2
    Last Post: 03-12-2002, 08:05 PM
  5. decimal to binary, decimal to hexadecimal and vice versa
    By Unregistered in forum C++ Programming
    Replies: 9
    Last Post: 12-08-2001, 11:07 PM