Thread: Integral Conversions

  1. #1
    Registered User
    Join Date
    Jun 2008
    Location
    Somewhere in Europe
    Posts
    99

    Integral Conversions

    K&R A6.2 says the following:

    "Any integer is converted to a given unsigned type by finding the smallest non-negative value that is congruent to that integer, modulo one more than the largest value that can be represented in the unsigned type. In a two's complement representation, this is equivalent to left-truncation if the bit pattern of the unsigned type is narrower, and to zero-filling unsigned values and sign-extending signed values if the unsigned type is wider."

    How does the second sentence follow from the first for negative numbers? More specifically, how do you determine the smallest non-negative value congruent to a given negative number?

    For example if you have

    Code:
    char c = -1;
    unsigned int i = c;
    what is going to happen exactly?

    In a two's complement representation, c will have the bit pattern 1111 1111, and so according to sentence 2, i will have the bit pattern 1111 ... 1111 (= (2**32) - 1) for sizeof(int) == 4.

    How would you get this result by the method of the first sentence? Presumably (2**32)-1 is the smallest non-negative value that is congruent to -1, modulo 2**32. But why is that the case?

    Any ideas?

  2. #2
    Hurry Slowly vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,788
    all the modulo sentence is about putting wider type inside type with less bits...

    for example if you write

    unsigned char u = x;

    value of x will be shortened by %256 (255 is biggest value represented by unsigned char)
    All problems in computer science can be solved by another level of indirection,
    except for the problem of too many layers of indirection.
    – David J. Wheeler

  3. #3
    Registered User
    Join Date
    Jun 2008
    Location
    Somewhere in Europe
    Posts
    99
    Are you sure about that? The way I read it, the whole of the second sentence (including the part about converting narrower types to wider types) relates to the first sentence.

  4. #4
    Hurry Slowly vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,788
    modulo - is about left-truncating... when you zero or sign filling - your modulo operator does nothing - returning same number
    All problems in computer science can be solved by another level of indirection,
    except for the problem of too many layers of indirection.
    – David J. Wheeler

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. How to convert integral types into pointer types?
    By rohit99 in forum C++ Programming
    Replies: 3
    Last Post: 03-20-2008, 09:57 AM
  2. Problem with Dawson's integral
    By lordbubonicus in forum C Programming
    Replies: 5
    Last Post: 11-25-2007, 03:44 PM
  3. Arithmetic conversions
    By wilksdr in forum C Programming
    Replies: 4
    Last Post: 10-02-2005, 09:10 PM
  4. why are enum type conversions disallowed?
    By krygen in forum C++ Programming
    Replies: 11
    Last Post: 11-24-2004, 11:22 AM
  5. Head Banging Floating Point Conversions
    By Davros in forum C++ Programming
    Replies: 9
    Last Post: 02-23-2004, 09:23 AM