MAths function with unsigned integer assigned a negative number

This is a discussion on MAths function with unsigned integer assigned a negative number within the C Programming forums, part of the General Programming Boards category; I am trying to convert a C programme to a scrip basic language. The C programme is fairly simple but ...

  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
    20,980
    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.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    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,831
    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-26-2009, 11:00 PM
  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, 04:01 PM

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