Pages: [1]   Go Down
Author Topic: Int variable using unsigned problem  (Read 632 times)
0 Members and 1 Guest are viewing this topic.
Portugal
Offline Offline
Edison Member
*
Karma: 37
Posts: 1545
Pretending you know everything then you will learn nothing.
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Hello today I get a weird output on a simple test skecth that is make me crazy.
Until today I believe on avr microcontroller an int was 2 bytes long and If I use the unsigned operator I just adjust the limits of the variable (-32765 to 35765 in case of signed and 0-65535 on a unsigned)
In this simple test that outputs on serial port the binary value of 2 ints declared on different ways I get a 32 bit on a variable like int var1;
Can someone explain me wait is happen here?
Code:
int intVar = 65535;
int  unsigned intVar2 = 65535;
int pin = 13;
volatile int state = LOW;



void setup()
{
  pinMode(pin, OUTPUT);
  Serial.begin(9600);
  attachInterrupt(0, blink, RISING);
}

void loop()
{
 Serial.print("Value in binary  without unsigned = ");
  Serial.println(intVar,BIN);
  Serial.print("Value in binary  with unsigned = ");
  Serial.println(intVar2,BIN);
  delay(2000);
}

void blink()
{
  state = !state;
  digitalWrite(pin, state);
}


The output on Serial Console:
Code:
Value in binary  without unsigned = 11111111111111111111111111111111
Value in binary  with unsigned = 1111111111111111
Value in binary  without unsigned = 11111111111111111111111111111111
Value in binary  with unsigned = 1111111111111111
Is the compiler turning it Long automatically?
« Last Edit: June 11, 2012, 05:16:01 pm by HugoPT » Logged

Debian,Mint,Ubuntu
Arduino Mega 2560
Arduino Nano
Arduino Duemilanove
MAC OS Montain Lion
Raspberry PI Model B


Offline Offline
Sr. Member
****
Karma: 1
Posts: 462
I am a amateur.
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

65535 is too large for a signed int variable (intVar). I think that it would cause intVar to do something weird. The first line of your serial output is 4,294,967,295 in decimal (upper limit of unsigned long), and the second line is fine.
Logged


Portugal
Offline Offline
Edison Member
*
Karma: 37
Posts: 1545
Pretending you know everything then you will learn nothing.
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

I believe the compiler convert it to an unsigned long automatically perhaps because I assign a value larger than 32765 on the signed var
using this
Code:
int intVar = 65535;
became unsigned long intVar ???
Another question is why the compiler do it automatically since I even not use any specifier unsigned or signed
« Last Edit: June 11, 2012, 05:29:13 pm by HugoPT » Logged

Debian,Mint,Ubuntu
Arduino Mega 2560
Arduino Nano
Arduino Duemilanove
MAC OS Montain Lion
Raspberry PI Model B


UK
Offline Offline
Faraday Member
**
Karma: 99
Posts: 4153
Where is your SSCCE?!?!
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

The answer lies in Print.cpp in the Arduino core:

Code:
size_t Print::print(int n, int base)
{
  return print((long) n, base);
}

When printing an int it promotes it to a long then calls the long printing routine.

A signed integer of 65535 is -1.  A long representation of -1 is 32 1's.
Logged

Get 10% off all 4D Systems TFT screens this month: use discount code MAJENKO10

UK
Offline Offline
Shannon Member
****
Karma: 223
Posts: 12577
-
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

This is due to signed extension. If you assign a signed variable with a negative value to another signed variable with a larger size, the value is sign extended by replicating the value of the most significant bit of the smaller variable into the extra bits available in the bigger variable. This is why int -1 and long -1 compare as equal although the bit representations are of course very different.
Logged

I only provide help via the forum - please do not contact me for private consultancy.

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

Quote
(-32765 to 35765 in case of signed and 0-65535 on a unsigned)
sp. "-32768 to 32767"
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.

Pages: [1]   Go Up
Jump to: