Thread: Question on hex numbers.

  1. #1
    Registered User
    Join Date
    Jul 2008
    Posts
    8

    Question on hex numbers.

    Hi guys,

    I have a hex number say 0x4C. What I have to do in C programming for 0x4C to split into two hex number 0x84 and 0x8C?

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    Well can you use bit manipulation to get to 0x04 and 0x0C for example?
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  3. #3
    Registered User
    Join Date
    Jul 2008
    Posts
    8
    Quote Originally Posted by Salem View Post
    Well can you use bit manipulation to get to 0x04 and 0x0C for example?
    I know I can do this: 0x45 & 0x0F = 0x0C

    But for 0x04 I still don't know how... Can you help me?

  4. #4
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by ahgan84
    I know I can do this: 0x45 & 0x0F = 0x0C
    Besides trying it out by using a calculator or writing a program, how do you know?
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  5. #5
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    As well as &, do you know about >>
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  6. #6
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Quote Originally Posted by ahgan84 View Post
    I know I can do this: 0x45 & 0x0F = 0x0C

    But for 0x04 I still don't know how... Can you help me?
    Actually 0x45 & 0x0F = 0x05

  7. #7
    Registered User
    Join Date
    Jun 2011
    Posts
    88
    Code:
    prompt% cat x.c ; gcc x.c -o a.out ; ./a.out
    #include <stdio.h> 
    int 
    main(int argc, char **argv, char **enver)
    {
       unsigned long x = 0x4C;
       unsigned long a = x ^ 0xC8;
       unsigned long b = x ^ 0xC0;
       printf("%lX %lX %lX\n", x, a, b);
       return(0);
    }
    4C 84 8C
    prompt%

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. question about enter only numbers
    By fugazi in forum C Programming
    Replies: 4
    Last Post: 10-22-2010, 02:42 PM
  2. Question regarding random numbers
    By girish1026 in forum C Programming
    Replies: 1
    Last Post: 01-03-2009, 03:44 AM
  3. Question about random numbers
    By Kempelen in forum C Programming
    Replies: 2
    Last Post: 07-02-2008, 06:28 AM
  4. Question about numbers
    By h3ro in forum C++ Programming
    Replies: 4
    Last Post: 05-14-2007, 09:42 AM
  5. line numbers question
    By InvariantLoop in forum C++ Programming
    Replies: 7
    Last Post: 04-16-2004, 08:08 AM