Thread: MAths function with unsigned integer assigned a negative number

  1. #1
    Registered User
    Join Date
    Apr 2011
    Posts
    6

    MAths function with unsigned integer assigned a negative number

    I am trying to convert a C programme to a scrip basic language.

    The C programme is fairly simple but produces unexpected results - it is the unxpected result that I would like to emulate with script basic.

    I am hoping someone can explain the mechanism that the C programme is using to come up with the unexpected result, then I will try and put that in practice in script basic using the available functions.

    The C programme:

    Code:
    main()
    {
    	unsigned int crc;
    		
    	crc=-248625283;
    	printf("crc before div by 256 %d\n", crc);
            printf("crc is a negative number to start with \n");	
    	crc=crc/256;
    	printf("crc after divide by 256 and assigned to an unsigned integer equals %d decimal \n", crc);
            
    }
    Normally if you divide -248625283 by 256 you would get a result of -971192 (integer)
    Similarly if you divide 248625283 by 256 you would get a result of 971192.

    The programme above gives a result of 15806023 . I expect this is related to assigning a negative number to a unsigned integer. What I need to know is how to get this result (15806023) using basic maths functions.

    I hope someone can help explain the mechanism that gives 15806023 when dividing -248625283 by 256.

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Search the Web concerning "two's complement", "one's complement" and "sign and magnitude". This will give you information on how the negative value would be converted to be stored in an unsigned integer, and from there working out the division is easy.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    Registered User
    Join Date
    Apr 2011
    Posts
    6
    Thanks laserlight it was the two's complement divided by 256. Fantastic, now to figure out how to do it in script basic.

  4. #4
    Registered User
    Join Date
    Sep 2008
    Location
    Toronto, Canada
    Posts
    1,834
    Assuming int has 32 bits, putting -248625283 into an unsigned int gives you 4046342013. Divide that by 256 = 15806023.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. [question]unsigned number with negative value?
    By learn in forum C Programming
    Replies: 2
    Last Post: 12-27-2009, 12:00 AM
  2. unsigned negative problem :s
    By nas_am in forum C++ Programming
    Replies: 1
    Last Post: 04-01-2009, 07:35 AM
  3. Assigning negative number to unsigned variables.
    By sar_mahesh in forum C Programming
    Replies: 10
    Last Post: 09-27-2006, 05:47 PM
  4. negative value assignment in an unsigned int
    By sanjeev_ddas in forum C Programming
    Replies: 1
    Last Post: 09-07-2006, 05:54 PM
  5. Negative value returned on unsigned int type
    By CHurst in forum C Programming
    Replies: 2
    Last Post: 12-10-2005, 05:01 PM