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