Serial.println with unsigne long

Hi all!

I am rading three bytes from my EEPROM. This is a time vale (in seconds). This I want to print out.
My code is as follow:

    high_byte=EEPROM1024.read(adress++);
    mid_byte=EEPROM1024.read(adress++);
    low_byte=EEPROM1024.read(adress++);
    result=(high_byte << 16) | (mid_byte << 8) | low_byte;
    Serial.print((unsigned long)result);

The three bytes are declared as bytes, result is declared as (unsigne long).
For the three bytes:0 7F F9 I get a 32761, which is OK. :slight_smile:
But for: 0 80 17 I am getting a 4294934551. :frowning:

Has anybody a idea, what's wrong? I am expecting to get a 32762 .

Regards
Hans

The most significan byte of 80 is expanded to the left as a sign bit.

4294934551 = FFFF8017 = 1111 1111 1111 1111 1000 0000 0001 0111

Can you post the declarations used for all the vars involved ?

Here are the declarations:

  unsigned long result;
  byte high_byte, mid_byte, low_byte;
  unsigned long adress;

try casting

    high_byte=EEPROM1024.read(adress++);
    mid_byte=EEPROM1024.read(adress++);
    low_byte=EEPROM1024.read(adress++);
    result=((long)high_byte << 16) | ((long)mid_byte << 8) | (long) low_byte;
    Serial.print((unsigned long)result);

(not tested)

Thank's a lot, your code is working!!!! :slight_smile: :slight_smile: :slight_smile: :slight_smile: :slight_smile:

I still not understanding my fault - but the important issue is: Now it's working!!!!!

Explanation - Arithmetic shift - Wikipedia
In computer programming, an arithmetic shift is a shift operator, sometimes known as a signed shift (though it is not restricted to signed operands). For binary numbers it is a bitwise operation that shifts all of the bits of its operand; every bit in the operand is simply moved a given number of bit positions, and the vacant bit-positions are filled in. Instead of being filled with all 0s, as in logical shift, when shifting to the right, the leftmost bit (usually the sign bit in signed integer representations) is replicated to fill in all the vacant positions (this is a kind of sign extension).

OK, I got it. Thanks'