Thread: Please help, basic unsigned conversion questions

  1. #1
    Registered User
    Join Date
    Feb 2005

    Please help, basic unsigned conversion questions

    The following is code which is run on a machine where values of type int are 32bits. They are represented in two's-complement, and right shifted arithmetically (meaning the bits are shift right, and the left bits are filled with the value of most significant bit). I'm assuming unsigned values are also 32 bits. Generating random values for x and y, I'm trying to find out whether the following expressions always yield a 1, or if there is a condition which makes the expression yield a 0.

    int x = random();
    int y = random();

    /* convert to unsigned */
    unsigned ux = (unsigned) x;
    unsigned uy = (unsigned) y;

    The following is what I have so far after testing with code:

    A) (x < y) == (-x > -y)
    False: If x= -2147483648 and y = 0, expression returns 0

    B) ((x+y) <<4) + y - x == 17 * y + 15 * x
    oddly enough I got False when x= 0 and y=0 ...not sure if that is correct nor the reasoning behind it though?

    C) ~x + ~y == ~(x+y)
    False for x=5 and y = 3

    D) (int) (ux-uy) == - (y - x)
    I think this is the only one I understand with some degree of is true in all circumstances. Two's complement and unsigned addition/subtraction have the same bit level representations so ux-uy = x - y and distributing the right side we also get x - y

    E) ((x >> 1) <<1) <= x
    Seems to hold true for all values of x, because if you do a right arithmetic shift by one bit and then shift it back one bit then it ends up having the value x , or less than x if x originally contains all 1's.

    Any corrections or help explaining any of the above would be greatly mind is spinning! Thanks
    Last edited by ninjacookies; 04-19-2005 at 10:19 PM.

  2. #2
    Registered User
    Join Date
    Feb 2005
    can anyone please help? please?


  3. #3
    Lead Moderator kermi3's Avatar
    Join Date
    Aug 1998
    Please do not bump posts. If you have more specific questions about your clearly homework based questions, you may post them. Otherwise, you can wait and see if people will do your homework for you.

    Further bumps will result in the thread being closed.

    If you're new to the boards, welcome and reading this will help you get started.
    Information on code tags may be found here

    - Sandlot is the highest form of sport.

  4. #4
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    The edge of the known universe
    > oddly enough I got False when x= 0 and y=0 ...not sure if that is correct nor the reasoning behind it though
    It's a fair bet that your test is wrong then.
    Think <<4 and *16 and work from there.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Basic Questions
    By Panda_ in forum C Programming
    Replies: 15
    Last Post: 09-23-2009, 04:24 PM
  2. Replies: 8
    Last Post: 06-04-2009, 02:03 PM
  3. basic questions about web services development
    By George2 in forum C# Programming
    Replies: 1
    Last Post: 07-13-2008, 09:30 AM
  4. unable to recieve UDP packet
    By caroundw5h in forum Networking/Device Communication
    Replies: 15
    Last Post: 09-19-2007, 11:11 AM
  5. can someone check this out and let me know ?
    By javaz in forum C Programming
    Replies: 5
    Last Post: 01-21-2002, 02:13 PM