Scunthorpe, UK
Offline
Full Member
Karma: 0
Posts: 124
"to make something better we have to break it"
|
 |
« on: October 07, 2012, 04:17:48 am » |
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
Melbourne, Australia
Offline
Shannon Member
Karma: 218
Posts: 13896
Lua rocks!
|
 |
« Reply #1 on: October 07, 2012, 04:20:48 am » |
|
|
|
|
|
Logged
|
|
|
|
|
Scunthorpe, UK
Offline
Full Member
Karma: 0
Posts: 124
"to make something better we have to break it"
|
 |
« Reply #2 on: October 07, 2012, 04:45:25 am » |
Ok.
|
|
|
|
|
Logged
|
|
|
|
|
|
|
Global Moderator
UK
Offline
Brattain Member
Karma: 137
Posts: 19030
I don't think you connected the grounds, Dave.
|
 |
« Reply #4 on: October 07, 2012, 04:59:11 am » |
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.
|
|
|
|
Scunthorpe, UK
Offline
Full Member
Karma: 0
Posts: 124
"to make something better we have to break it"
|
 |
« Reply #5 on: October 07, 2012, 05:09:04 am » |
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
Faraday Member
Karma: 78
Posts: 5453
Strongly opinionated, but not official!
|
 |
« Reply #6 on: October 07, 2012, 05:19:38 am » |
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
Melbourne, Australia
Offline
Shannon Member
Karma: 218
Posts: 13896
Lua rocks!
|
 |
« Reply #7 on: October 07, 2012, 05:27:07 am » |
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
Melbourne, Australia
Offline
Shannon Member
Karma: 218
Posts: 13896
Lua rocks!
|
 |
« Reply #8 on: October 07, 2012, 05:27:37 am » |
Or your output.
|
|
|
|
|
Logged
|
|
|
|
|
Scunthorpe, UK
Offline
Full Member
Karma: 0
Posts: 124
"to make something better we have to break it"
|
 |
« Reply #9 on: October 07, 2012, 02:22:09 pm » |
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. 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
Brattain Member
Karma: 137
Posts: 19030
I don't think you connected the grounds, Dave.
|
 |
« Reply #10 on: October 07, 2012, 02:27:03 pm » |
Really useful to match printed label to type.
|
|
|
|
|
Logged
|
Pete, it's a fool looks for logic in the chambers of the human heart.
|
|
|
|
Global Moderator
Melbourne, Australia
Offline
Shannon Member
Karma: 218
Posts: 13896
Lua rocks!
|
 |
« Reply #11 on: October 07, 2012, 02:59:08 pm » |
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
Full Member
Karma: 0
Posts: 124
"to make something better we have to break it"
|
 |
« Reply #12 on: October 07, 2012, 03:10:39 pm » |
Sorry Nick. Here is what i see in the serial monitor below.
unsignd int value 1111111001111111 int value 11111111111111111111111001111111
|
|
|
|
|
Logged
|
|
|
|
|
Global Moderator
UK
Offline
Brattain Member
Karma: 137
Posts: 19030
I don't think you connected the grounds, Dave.
|
 |
« Reply #13 on: October 07, 2012, 03:12:52 pm » |
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.
|
|
|
|
Global Moderator
Melbourne, Australia
Offline
Shannon Member
Karma: 218
Posts: 13896
Lua rocks!
|
 |
« Reply #14 on: October 07, 2012, 03:23:11 pm » |
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.
|
|
|
|
|
Logged
|
|
|
|
|
|