Go Down

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

Pavilion1984

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.

Nick Gammon

Please post technical questions on the forum, not by personal message. Thanks!

More info:
http://www.gammon.com.au/electronics


Pavilion1984

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

AWOL

The size of an int is platform-dependent.
On the Arduino, it is two bytes.
"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.

Pavilion1984

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

westfw

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

Nick Gammon


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.
Please post technical questions on the forum, not by personal message. Thanks!

More info:
http://www.gammon.com.au/electronics

Nick Gammon

Please post technical questions on the forum, not by personal message. Thanks!

More info:
http://www.gammon.com.au/electronics

Pavilion1984

#9
Oct 07, 2012, 09:22 pm Last Edit: Oct 07, 2012, 09:34 pm by Pavilion1984 Reason: 1
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.

Code: [Select]
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).

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?
Please post technical questions on the forum, not by personal message. Thanks!

More info:
http://www.gammon.com.au/electronics

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.
Please post technical questions on the forum, not by personal message. Thanks!

More info:
http://www.gammon.com.au/electronics

Go Up