Go Down

Topic: int and unsigned int (Read 3 times) previous topic - next topic

AWOL

Really useful to match printed label to type.
"Pete, it's a fool looks for logic in the chambers of the human heart." Ulysses Everett McGill.
Do not send technical questions via personal messaging - they will be ignored.

Nick Gammon


Or your output.


To save me compiling, and uploading, how about you show your output? Then we can see what you are talking about? Don't just describe it.

The whole thread is complaining about what you see in the serial monitor. Why not post what you see?

Pavilion1984

Sorry Nick. Here is what i see in the serial monitor below.

unsignd int value
1111111001111111
int value
11111111111111111111111001111111

AWOL

I'm a way from the source at the moment, but reply #6 seems plausible.
"Pete, it's a fool looks for logic in the chambers of the human heart." Ulysses Everett McGill.
Do not send technical questions via personal messaging - they will be ignored.

Nick Gammon

Westfw is right, I think. The actual printing method is this:

Code: [Select]

// Private Methods /////////////////////////////////////////////////////////////

size_t Print::printNumber(unsigned long n, uint8_t base) {
  char buf[8 * sizeof(long) + 1]; // Assumes 8-bit chars plus zero byte.
  char *str = &buf[sizeof(buf) - 1];

  *str = '\0';

  // prevent crash if called with base == 1
  if (base < 2) base = 10;

  do {
    unsigned long m = n;
    n /= base;
    char c = m - base * n;
    *--str = c < 10 ? c + '0' : c + 'A' - 10;
  } while(n);

  return write(str);
}


Other methods cast and muck around, but end up here. By sign extending into the unsigned long argument you will end up with extra "1" bits.

Go Up