Pages: [1] 2   Go Down
Author Topic: int and unsigned int  (Read 2767 times)
0 Members and 1 Guest are viewing this topic.
Scunthorpe, UK
Offline Offline
Full Member
***
Karma: 0
Posts: 129
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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

Global Moderator
Offline Offline
Brattain Member
*****
Karma: 452
Posts: 18694
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Read this before posting a programming question
Logged

Scunthorpe, UK
Offline Offline
Full Member
***
Karma: 0
Posts: 129
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Ok.
Logged

Scunthorpe, UK
Offline Offline
Full Member
***
Karma: 0
Posts: 129
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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

Global Moderator
UK
Offline Offline
Brattain Member
*****
Karma: 239
Posts: 24371
I don't think you connected the grounds, Dave.
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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

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

Scunthorpe, UK
Offline Offline
Full Member
***
Karma: 0
Posts: 129
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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

SF Bay Area (USA)
Offline Offline
Tesla Member
***
Karma: 106
Posts: 6374
Strongly opinionated, but not official!
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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

Global Moderator
Offline Offline
Brattain Member
*****
Karma: 452
Posts: 18694
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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

Global Moderator
Offline Offline
Brattain Member
*****
Karma: 452
Posts: 18694
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Or your output.
Logged

Scunthorpe, UK
Offline Offline
Full Member
***
Karma: 0
Posts: 129
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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:
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).
« Last Edit: October 07, 2012, 02:34:55 pm by Pavilion1984 » Logged

Global Moderator
UK
Offline Offline
Brattain Member
*****
Karma: 239
Posts: 24371
I don't think you connected the grounds, Dave.
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Really useful to match printed label to type.
Logged

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

Global Moderator
Offline Offline
Brattain Member
*****
Karma: 452
Posts: 18694
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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?
Logged

Scunthorpe, UK
Offline Offline
Full Member
***
Karma: 0
Posts: 129
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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

unsignd int value
1111111001111111
int value
11111111111111111111111001111111
Logged

Global Moderator
UK
Offline Offline
Brattain Member
*****
Karma: 239
Posts: 24371
I don't think you connected the grounds, Dave.
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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

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

Global Moderator
Offline Offline
Brattain Member
*****
Karma: 452
Posts: 18694
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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

Code:
// 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.
Logged

Pages: [1] 2   Go Up
Jump to: