INTS AND LONGS - HIT THE BUFFERS

Evening All,

This one is driving me bonkers. I thought that longs took bigger numbers than ints, but not in my case. This is all to do with extracting bytes from 9 digit numbers to send to memory (I2C EEPROM), reading back and reassembling the original number. I hope the code and comments make sense, but ints work and longs don't. Tried everything, but hit the buffers. Just change the unsigned ints to unsigned longs to get the problem. The newLowWord seems to be the spanner in the works.

/*SHOULD GIVE A FINAL RESULT OF 999999999 (9DIGITS)
UNSIGNED INT WORKS FOR NEWLOWWORD AND NEWHIGHWORD, BUT UNSIGNED LONG DOES NOT
*/
unsigned long recombinelongValue2=0;
unsigned int newLowWord;
unsigned int newHighWord;

void setup(){
 Serial.begin(9600);

  newLowWord= 255*1+201*256;
  Serial.print("newLowWord");
  Serial.println(newLowWord);
  
  newHighWord =59*256+154*1;
  Serial.print("newHighWord");
  Serial.println(newHighWord);
  
  recombinelongValue2=newLowWord+newHighWord*65536;
  Serial.println("recombinelongValue2");
  Serial.println(recombinelongValue2);
}
void loop()
{
}

Did you try type modifiers for the constants, e.g. instead of "201" or "256" using "201UL" and "256UL"?

Try this

unsigned long recombinelongValue2 = 0;
unsigned long newLowWord;
unsigned long newHighWord;

void setup(){
 Serial.begin(115200);

  newLowWord = (255UL * 1) + (201UL * 256);
  Serial.print("newLowWord ");
  Serial.println(newLowWord);
  
  newHighWord = (59UL * 256) + (154UL * 1);
  Serial.print("newHighWord ");
  Serial.println(newHighWord);
  
  recombinelongValue2 = newLowWord + (newHighWord * 65536UL);
  Serial.print("recombinelongValue2 ");
  Serial.println(recombinelongValue2);
}

Output newLowWord 51711 newHighWord 15258 recombinelongValue2 999999999

Integer arithmetic and overflow

Thank you all.

Igendel's suggestion worked fine, as does UK Bob. This is all self-taught stuff on my part and I'm too old now to ever be a decent programmer. I have used UL suffixes before, but assumed that as the named variables had been declared as unsigned longs, I wouldn't have to do it again. Actually trying to find a reference to this had not come up with an answer - must remember to have a look at NG's site again. For what it's worth, this is part of a project to get gas meter readings onto a display in the house. This problem cropped up when I wanted a method to store the readings during a power fail. So, starting from nil knowledge, I gone through the hardware side (Hall effect), XBee series 1 line passing, etc.,etc. Plus spare time is in short supply.

Thanks again