int and unsigned int

Hello all,

This is probably a silly question but am going to ask it. I thought an int was 16bits long but when i print out a int value in the serial monitor as binary i can see 32bits. If i change the value to unsigned int i then see just 16bits. Why is this? Is it because the int type can hold a negative number but the unsigned int can't?

Thanks.

Read this before posting a programming question

Ok.

Arduino reference says int 2bytes but if i look here http://www.tutorialspoint.com/cplusplus/cpp_data_types.htm an int is 4bytes.

The size of an int is platform-dependent. On the Arduino, it is two bytes.

Thank you, that explains a few different websites. So am i right in thinking in Arduino a int is 32bits long and an unsigned int is 16bits long? Well from what i can see in the serial monitor it is. Sorry for the so called dumb questions but i have never used any programing till a few months ago and am tring to learn myself Arduino and actual C++.

No, I think you just found a minor bug in the "binary" option of print. It casts all ints to longs before it actually prints them, which becomes a bit odd for negative numbers...

Pavilion1984: I thought an int was 16bits long but when i print out a int value in the serial monitor as binary i can see 32bits.

I still don't see your code.

Or your output.

Here is my test code that shows in the serial monitor what am talking about, displays a int value that has 32bits and an unsigned int that has 16bits.

byte temp;
unsigned int result = 7000;
unsigned int Display_RPM_A;
int Display_RPM_B;

void setup() {
  // put your setup code here, to run once:
  Serial.begin(9600);
  
  temp = result/1000;
  Display_RPM_A = ~((1 << (16 - temp))-1) | ((1 << temp)-1);
  
  temp = result/1000;
  Display_RPM_B = ~((1 << (16 - temp))-1) | ((1 << temp)-1);
  
  Serial.println("unsigned int value");
  Serial.println(Display_RPM_A, BIN);
  
  Serial.println("int value");
  Serial.println(Display_RPM_B, BIN);
}

void loop() {
  // put your main code here, to run repeatedly: 
  
}

(Changed code above because the wrong lable was being printed).

Really useful to match printed label to type.

[quote author=Nick Gammon link=topic=126002.msg947434#msg947434 date=1349605657] Or your output. [/quote]

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?

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

unsignd int value 1111111001111111 int value 11111111111111111111111001111111

I'm a way from the source at the moment, but reply #6 seems plausible.

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

// 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.

The unsigned int variable or delcared as a word is what i was wanting to create and works great in my main code. But because at first i had the variable "Display_RPM" delcared as just an int and seeing 32bits in the serial monitor just didn't add up. The documentation for a int value in the reference section says it's meant to be 16bits not 32bits that am seeing in the serial monitor. So because am a beginner at C++ and Arduino this was very confusing for me and thought my understanding was wrong, hence why i thought it was a silly question in the first place.

How do you report a bug?

Why are you looking at it in binary in the first place?

Who wants to see RPM in binary?

How do you report a bug?

http://code.google.com/p/arduino/issues/list

And how did you manage to get negative RPM? It's not clear to me what the "correct" method of displaying a negative number in binary would be, or whether it would be worth the special case code to limit the binary display to 16bits for an "int."

Why are you looking at it in binary in the first place?

Who wants to see RPM in binary?

Because i don’t have a shift register at the moment so i was using the serial monitor to to show me what the output / effect would look like if i did send the information to a shift register. 1 = LED on and 0 = LED off and i will have 16 LED’s.

[And how did you manage to get negative RPM?
It’s not clear to me what the “correct” method of displaying a negative number in binary would be, or whether it would be worth the special case code to limit the binary display to 16bits for an “int.”

It’s not a negative number because my variable is a unsigned int / word and they don’t hold negative numbers according to the reference section. If i set my variable Display_RPM to an int and display the created value in the serial monitor, then i get a negative number. If i set my variable to unsigned int/word then i get a positive number and that is what i want.

#define pin A0 //Pot connected for testing.

byte temp;
unsigned int Display_RPM;
unsigned int result;

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

void loop()
{
  result = map(analogRead(pin), 0, 1024, 0, 8000); //Maps the pot value to simulate RPM value.

  temp = result/1000;
  Display_RPM = ~((1 << (16 - temp))-1) | ((1 << temp)-1);
  Serial.println(Display_RPM, BIN);
  Serial.println(Display_RPM);
  delay(900);
}

1 = LED on, 0 + LED off
below is what i think i would see on a 16bit shift register when i buy one.

1000rpm = 1000000000000001
2000rpm = 1100000000000011
3000rpm = 1110000000000111
4000rpm = 1111000000001111
5000rpm = 1111100000011111
6000rpm = 1111110000111111
7000rpm = 1111111001111111
8000rpm = 1111111111111111

Does that explain everything now.

If i set my variable Display_RPM to an int and display the created value in the serial monitor,

I think you're confusing how a number is represented internally with how the Serial object happens to present it.