Thread: Any suggestions for finding the largest possible value of INT?

  1. #16
    Registered User
    Join Date
    Sep 2006
    Posts
    835
    > Where is the ~0UL coming from, is it part ot stdbool or something else.

    It has nothing to do with <stdbool.h>. The UL is a suffix that makes the constant into an unsigned long. An integral constant without a suffix such as 57 is a (signed) int by default, while 57U is an unsigned int, and 57UL is an unsigned long. A floating-point constant such as 1.0 with a decimal point but no suffix is double by default, while 1.0F is a float, and 1.0L is a long double.

    Edit: This is not a complete list, and you can also use the lower-case version of these letters, though the upper-case version is usually easier to read properly, for example 57L is a long int, and so is 57l, but the latter looks like 571, so you should use L.
    Last edited by robatino; 09-05-2007 at 11:02 PM.

  2. #17
    Registered User
    Join Date
    Mar 2007
    Posts
    23
    Got it! Thanks, I wrote out some sample code and tested the effects. I don't think I have ever read about that before, handy to know.

  3. #18
    Registered User
    Join Date
    Mar 2007
    Posts
    23
    I have not seen the ~0 or ~value before, where is that coming from? I have seen ~1 used to set boolean values before. I see how it works and it seems straigh forward but I would like to know more about what it is. What would I call setting unsigned int x = ~0u; ?

    I looked though my Kernighan and Ritchie 2nd edition and I don't see it.
    Last edited by zensatori; 09-05-2007 at 11:20 PM.

  4. #19
    Registered User
    Join Date
    Sep 2006
    Posts
    835
    See section 2.9 (Bitwise Operators) on page 48.

  5. #20
    Registered User
    Join Date
    Mar 2007
    Posts
    23

    Wink

    Quote Originally Posted by robatino View Post
    See section 2.9 (Bitwise Operators) on page 48.
    Oh, so it's one's complement? I'll have to re-read that section. I really need to get a copy of "The C Answer Book"

    Thanks again, I really appreciate the help.

    This is such a great book, I wish every language had something as good.
    Last edited by zensatori; 09-05-2007 at 11:31 PM.

  6. #21
    Officially An Architect brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,396
    Quote Originally Posted by zensatori View Post
    Oh, so it's one's complement? I'll have to re-read that section. I really need to get a copy of "The C Answer Book"
    Bitwise operators do not depend on complement. The "complement" refers to the way negative values are represented. Bitwise operators manipulate the bits themselves.

  7. #22
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,318
    It is as simple as this:
    0 always has all bits as zero
    ~ always negates all bits
    therefore with ~0, all bits are ones.

    Once you've got a number where all bits are ones, how the processor interprets that when you do things with it is a different story, and that's where number representations come into play.
    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"

  8. #23
    Registered User
    Join Date
    Sep 2006
    Posts
    835
    If the 0 is a signed type, then in two's complement, all bits are zero, but in ones' complement and sign-magnitude, it has two representations, plus zero and minus zero, so this is no longer true. So to be portable, one has to make the 0 unsigned, either with an unsigned suffix, or an explicit cast, before applying ~.

    Edit: Actually, I'm not 100&#37; sure about this - I don't know if the standard says anything about whether the bit representation of plain signed int 0 is all zeroes or not. Anyone know?
    Last edited by robatino; 09-06-2007 at 02:03 AM.

  9. #24
    Technical Lead QuantumPete's Avatar
    Join Date
    Aug 2007
    Location
    London, UK
    Posts
    894
    Quote Originally Posted by robatino View Post
    plus zero and minus zero
    oh god, don't tell me 0 can be signed as well? I miss those days in primary school when all the number of the world could be counted off your fingers...

    QuantumPete
    Last edited by QuantumPete; 09-06-2007 at 02:12 AM.
    "No-one else has reported this problem, you're either crazy or a liar" - Dogbert Technical Support
    "Have you tried turning it off and on again?" - The IT Crowd

  10. #25
    Registered User
    Join Date
    Sep 2006
    Posts
    835
    Not only in ones' complement or sign-magnitude signed integral arithmetic, but in floating-point as well - see

    http://en.wikipedia.org/wiki/IEEE_754

    which is the most common floating-point format.

  11. #26
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Is there any machines currently in existance (aside from obscure Cray's or IBM and Data General machines from 1970's - and, ok, some graphics processors may not use this either, but I'm thinking of machines where you can run C or C++-code) where -1 is NOT all ones?

    --
    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.

  12. #27
    Registered User
    Join Date
    Sep 2006
    Posts
    835
    Well, the C and C++ standards still allow ones' complement and sign-magnitude signed integral arithmetic, so there must be a few of them out there. If they all die, the standard can start requiring two's complement arithmetic (as in Java, for example).

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. Game Won't Compile
    By jothesmo in forum C++ Programming
    Replies: 2
    Last Post: 04-01-2006, 04:24 PM
  3. Replies: 2
    Last Post: 03-24-2006, 08:36 PM
  4. getting a headache
    By sreetvert83 in forum C++ Programming
    Replies: 41
    Last Post: 09-30-2005, 05:20 AM
  5. Replies: 4
    Last Post: 11-23-2003, 07:15 AM