Thread: why is int 0xFFFFFFFF is -1 ?

  1. #1
    Registered User
    Join Date
    Aug 2014
    Posts
    26

    why is int 0xFFFFFFFF is -1 ?

    can someone make it clear for me please ?

    if i say
    Code:
    int a = 0xFFFFFFFF;
    unsigned int b = 0xFFFFFFFF;
    
    printf("a = %d, b = %u" , a,b);
    the output will be :
    a = -1 , b = 4294967295

    // now . FFFF FFFF is 1111 1111 and 'int a' has the first 1 as the flag for negetive number .
    . how its works. someone can figure it out for me ?
    Last edited by Idan Damri; 08-18-2014 at 07:13 AM.

  2. #2
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Simply put, 0xFF = 1111 1111 is the 2s complement for -1. Look up 2s complement. It's an encoding scheme used in computers.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  3. #3
    Registered User
    Join Date
    Jun 2005
    Posts
    6,815
    Note, also, that C implementations are not required to use 2s-complement encoding - and there are certainly implementations that don't. The result could therefore be different with different implementations (compiler, host system, etc).
    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
    Registered User
    Join Date
    Oct 2006
    Posts
    3,445
    Quote Originally Posted by Elysia View Post
    Look up 2s complement. It's an encoding scheme used in computers.
    Also be aware that true 2s-complement has a representation for negative zero, which is distinct from positive zero. The modified 2s-complement used in most systems you're likely to encounter does not.
    What can this strange device be?
    When I touch it, it gives forth a sound
    It's got wires that vibrate and give music
    What can this thing be that I found?

  5. #5
    Master Apprentice phantomotap's Avatar
    Join Date
    Jan 2008
    Posts
    5,108
    Also be aware that true 2s-complement has a representation for negative zero, which is distinct from positive zero.
    O_o

    I think you are looking for "One's Complement" where "Two's Complement" is the one without the negative zero.

    Soma
    “Salem Was Wrong!” -- Pedant Necromancer
    “Four isn't random!” -- Gibbering Mouther

  6. #6
    Registered User
    Join Date
    Oct 2006
    Posts
    3,445
    You're right. My mistake.
    What can this strange device be?
    When I touch it, it gives forth a sound
    It's got wires that vibrate and give music
    What can this thing be that I found?

  7. #7
    Registered User
    Join Date
    Jan 2014
    Posts
    45
    To be clear, 0xffffffff is not -1, it is 4294967295. This value may or may not be representable by an int or an unsigned int (see 5.2.4.2.1p1). When the value cannot be represented by an int, converting it to an int has implementation-defined behaviour (see 6.3.1.3p3).

  8. #8
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Quote Originally Posted by zyxwvuts View Post
    To be clear, 0xffffffff is not -1, it is 4294967295...
    No, it's not. A number is a string of bits. What number those string of bits represents depends on how you interpret them.
    If you interpret it as an unsigned number, then it's 4..294..967..295.
    If you interpret it as a two's complement number, then it's -1.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  9. #9
    Registered User
    Join Date
    Jan 2014
    Posts
    45
    The point I was trying to convey is that C will interpret the integer constant (not the representation) 0xffffffff as having the value that is commonly written as 4294967295. (See 6.4.4.1p4.)

  10. #10
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,318
    Quote Originally Posted by zyxwvuts View Post
    The point I was trying to convey is that C will interpret the integer constant (not the representation) 0xffffffff as having the value that is commonly written as 4294967295. (See 6.4.4.1p4.)
    That's just it though, it doesn't because int's are signed.
    0xffffffffU does though.
    My homepage
    Advice: Take only as directed - If symptoms persist, please see your debugger

    Linus Torvalds: "But it clearly is the only right way. The fact that everybody else does it some other way only means that they are wrong"

  11. #11
    Master Apprentice phantomotap's Avatar
    Join Date
    Jan 2008
    Posts
    5,108
    That's just it though, it doesn't because int's are signed.
    O_o

    The "signedness" of `int' has nothing to do with the comment zyxwvuts posted.

    Integer literals are not necessarily `int'; they are "first fit" according to a few rules.

    If the compiler supports larger--such as 64bit--values, the value `0xffffffff' as an `int' is naturally 4294967295 decimal.

    When a compiler is limited smaller--such as 32bit--values, the value `0xffffffff' may be too large for a `int'--which is signed. A conforming compiler will then try `unsigned int', `long', and so on until an appropriate type is found. If one of the unsigned types "first fit", the value of `0xffffffff' would be treated as unsigned thus still being 4294967295 decimal.

    [Edit]
    Tried to clear up some awkward wording...
    [/Edit]

    Soma
    Last edited by phantomotap; 08-24-2014 at 01:38 AM.
    “Salem Was Wrong!” -- Pedant Necromancer
    “Four isn't random!” -- Gibbering Mouther

Popular pages Recent additions subscribe to a feed