Thread: half an int

  1. #1
    Registered User
    Join Date
    Feb 2002
    Posts
    114

    half an int

    Hello all, just a quick quest:

    is half an unsigned int 0x80000000 or 0x7FFFFFFF?

    0xFFFFFFFF / 2 = 0x7FFFFFFF

    but

    0x7FFFFFFF * 2 = 0xFFFFFFFE

    (note the trailing E)

    and

    0x80000000 * 2 = 0x0 (or more exactly 0x100000000 (9 digits))

    I opt for 0x7FFFFFFF (as 2 * the half shouldnt be 0!)

    Any comments or thoughts?

  2. #2
    Programming Sex-God Polymorphic OOP's Avatar
    Join Date
    Nov 2002
    Posts
    1,078
    0x7FFFFFFF

    would be "half" of an unsigned 32 bit integer, though "half" is a rather silly term for it. A 32 integer holds an even number of values, but you have to remember that the values start at 0 not 1, so dividing directly by 2 will give you a fraction. What you'd do is divide 2^32 by 2 and then subtract 1 from it to get "half" full. Since the "value" of 0xFFFFFFFF is odd, you can't multiply an integer by 2 to get it.

    The reason you are getting 0 for 0x80000000 * 2 is because that goes 1 over the maximum amount that a 32 bit integer can hold. The numbers "rolled over" back to 0.
    Last edited by Polymorphic OOP; 12-16-2002 at 02:28 AM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. NEED HELP READING FILE and PRINTING
    By geoffr0 in forum C Programming
    Replies: 4
    Last Post: 04-16-2009, 05:26 PM
  2. Screwy Linker Error - VC2005
    By Tonto in forum C++ Programming
    Replies: 5
    Last Post: 06-19-2007, 02:39 PM
  3. Quack! It doesn't work! >.<
    By *Michelle* in forum C++ Programming
    Replies: 8
    Last Post: 03-02-2003, 12:26 AM
  4. easy if you know how to use functions...
    By Unregistered in forum C Programming
    Replies: 7
    Last Post: 01-31-2002, 07:34 AM
  5. How do you search & sort an array?
    By sketchit in forum C Programming
    Replies: 30
    Last Post: 11-03-2001, 05:26 PM