Integral Conversions

This is a discussion on Integral Conversions within the C Programming forums, part of the General Programming Boards category; K&R A6.2 says the following: "Any integer is converted to a given unsigned type by finding the smallest non-negative value ...

  1. #1
    DL1
    DL1 is offline
    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
    CSharpener vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,484
    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)
    The first 90% of a project takes 90% of the time,
    the last 10% takes the other 90% of the time.

  3. #3
    DL1
    DL1 is offline
    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
    CSharpener vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,484
    modulo - is about left-truncating... when you zero or sign filling - your modulo operator does nothing - returning same number
    The first 90% of a project takes 90% of the time,
    the last 10% takes the other 90% of the time.

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, 02: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, 10:22 AM
  5. Head Banging Floating Point Conversions
    By Davros in forum C++ Programming
    Replies: 9
    Last Post: 02-23-2004, 08:23 AM

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