Go Down

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


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

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.

Nick Gammon


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

Go Up