Thread: long int to int

  1. #1
    Registered User
    Join Date
    Feb 2008
    Posts
    3

    long int to int

    Hi,
    I have a long int:

    unsigned long int x = 0xFFFFFFFFAAAAAAAA;

    I want to get

    unsigned int low;
    unsigned int high;

    such that

    low = 0xAAAAAAAA
    high = 0xFFFFFFFF

    I tried a bit shift and it doesn't work with MSVC but it does work with RVCT (ARM compiler). Is there a standard lib function I can use?
    Thanks

  2. #2
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    What does your actual code look like?

    How about something like this:
    Code:
    union u
    {
        unsigned long long ll;
        struct two_int
        {
           unsigned high;
           unsigned low;
        };
    };
    
    ...
       u.ll = longValue;
       cout << u.high;
       cout << u.low;
    ...
    Edit: The order of high/low depends on whether your machine is big or little endian, so you may need to swap them.

    --
    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
    Registered User
    Join Date
    Jun 2005
    Posts
    6,815
    Apart from endianness, there is also the problem that int (signed or unsigned) is the same size as long with some compilers. MSVC is one of those.

    Make low and high of type unsigned short, rather than unsigned int. And, preferably, confirm that sizeof(unsigned long) == sizeof(unsigned short)*2.
    Right 98% of the time, and don't care about the other 3%.

    If I seem grumpy or unhelpful in reply to you, or tell you you need to demonstrate more effort before you can expect help, it is likely you deserve it. Suck it up, Buttercup, and read this, this, and this before posting again.

  4. #4
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Grumpy, in principle I agree. But the resolution seems a bit wrong:

    The values shown indicate a 64-bit "long" and a 32-bit "int", so if that's what the original poster wants, then we would be looking at using a 64-bit type for the "long" value (e.g. long long in my example), and a unsigned int for the 32-bit value.

    --
    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. Debug Error Really Quick Question
    By GCNDoug in forum C Programming
    Replies: 1
    Last Post: 04-23-2007, 12:05 PM
  2. About aes
    By gumit in forum C Programming
    Replies: 13
    Last Post: 10-24-2006, 03:42 PM
  3. Converted from Dev-C++ 4 to Dev-C++ 5
    By Wraithan in forum C++ Programming
    Replies: 8
    Last Post: 12-03-2005, 07:45 AM
  4. Switch/case Problems (long code in post)
    By Wraithan in forum C++ Programming
    Replies: 2
    Last Post: 12-01-2005, 06:40 PM
  5. easy if you know how to use functions...
    By Unregistered in forum C Programming
    Replies: 7
    Last Post: 01-31-2002, 07:34 AM