long int to int

This is a discussion on long int to int within the C++ Programming forums, part of the General Programming Boards category; Hi, I have a long int: unsigned long int x = 0xFFFFFFFFAAAAAAAA; I want to get unsigned int low; unsigned ...

  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,245
    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%.

  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, 06:45 AM
  4. Switch/case Problems (long code in post)
    By Wraithan in forum C++ Programming
    Replies: 2
    Last Post: 12-01-2005, 05:40 PM
  5. easy if you know how to use functions...
    By Unregistered in forum C Programming
    Replies: 7
    Last Post: 01-31-2002, 06:34 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21