16-bit handles 32/64-bit weirdly

DONT MIND ALL THIS

I found a small problem that is more or less frustrating...

The ATmega328 is 16-bit and can therefore handle numbers up to 65'535, but not further.
The long data type requires 32-bit so the CPU handles it a bit differently.
(Theres also a long long data type which is 64-bit, but the same problem appears on that one)

If I declare a long long variable I can use it to calculate all and all no big problems
long long num = 5000;
num += 2000;
Serial.println(word(num));
Gives out 7000 in respond.

But I have to convert the number to string to send it, therefore the word() function. But even if I print it on a LCD or anything it still got the problem.
Now the problem is as follows: if the number is above 65535 it "resets" back to 0, or if you got some rest it gets added. For example
65535L + 5L
gives 4 (it includes the 0) because it's actually 65536 (2^16) + 4.

You can check the "real" value for example
floor( 80000L / 65536L )
gives 1, so they are not exactly reset; it's just that when you get the string form of the long it can't handle numbers bigger then 65535 (which is max from 16-bit, aka 11111111 11111111 in binary is 65535 in decimal).

The ATmega328 is 16-bit and can therefore handle numbers up to 65'535, but not further.

No it's not it is an 8bit CPU.
So any limits are down to the code not the CPU

Word(num) casts your variable to a word datatype, the same as an int on Arduino.

It's nothing to do with strings.

Your code is working perfectly. It's you who are in error. :wink:

Grumpy_Mike:
No it's not it is an 8bit CPU.

I bet you're right. Then the code gotta be 16 bit or something. I still got this 16-bit problem

tack:
Word(num) casts your variable to a word datatype, the same as an int on Arduino.

It's nothing to do with strings.

Well ok wtf, I have no clue what I am doing. I am a noob to C++ (or whatever bloody language this is); how do I get this to work?!

akaJag:

tack:
Word(num) casts your variable to a word datatype, the same as an int on Arduino.

It's nothing to do with strings.

Well ok wtf, I have no clue what I am doing. I am a noob to C++; how do I get this to work?!

Serial.println(num);

Where did you get that code from? What made you think word () is anything to do with converting to strings?

I would suggest starting with the Arduino Language Reference.

tack:

serial.Println (num);

What? I should note that I can not use the serial because my IDE crashes when it gets multiple serial messages (I do not know why nor how to fix it). I am using my raspberry pi because my windows cant launch the IDE.

Why did you show a Serial.print example in your first post then and quote certain outputs from the sketch you posted?

tack:
Why did you show a serial.Print example in your first post then and quote certain outputs from the sketch you posted?

BECAUSE I am not wondering how to fix my serial shit. I gave you the principal and the question on how to fix this problem. Please stay related to my main question here!

akaJag:

tack:
Why did you show a serial.Print example in your first post then and quote certain outputs from the sketch you posted?

BECAUSE I am not wondering how to fix my serial shit. I gave you the principal and the question on how to fix this problem. Please stay related to my main question here!

I gave you the answer. You are in error. word() is a cast, nothing to do with strings. You cast your variable to word datatype (16 bit) before output, so why would it be anything else?

With that attitude, I'm outta here. Good luck with any future help. I don't owe you my free time so I will donate it to someone more appreciative.

tack:
I gave you the answer. You are in error. word() is a cast, nothing to do with strings. You cast your variable to word datatype (16 bit) before output, so why would it be anything else?

With that attitude, I'm outta here. Good luck with any future help. I don't owe you my free time so I will donate it to someome more appreciative.

I am bloody sorry but I am sure you know how someones attitude can get messy when they are frustrated. I just bought this arduino kit and almost nothing of it is working.

Now I removed all the word() stuff but I am still getting the problem where it is going to 0 when it reaches 65536.

With some nooby testing I just used String() and now it works. You do no longer need to spend time on this post

Post code.
Stop hand-waving.

Don't you think it remarkable that no-one else has complained about the Arduino's handling of 8/16/32/64 bit variables?

Please mind the language too.

Edit:

With some nooby testing I just used String()

Here we go...

AWOL:
--snip--

If you dislike me asking questions I can stop. I will search for help elsewhere.
I don't know what really to say here.

AWOL:
Edit:

With some nooby testing I just used String()

Here we go...

And please don't look down on me like that

Ok well now I am really stuck and please help...
String() cannot convert long long to string, is there a alternative function I can use?

See reply 12 - the bit about "post code".

AWOL:
See reply 12 - the bit about "post code".

Excuse my english knowledge, but what does post code mean?

You can see your sketch - we can't.
So, post it here.
Use code tags.

AWOL:
You can see your sketch - we can't.
So, post it here.
Use code tags.

Oh as simple as that, this is my current code

#include <LiquidCrystal.h>
LiquidCrystal lcd(12,11,5,4,3,2);

long long testNum;

void setup(){
  lcd.begin(16,2);
  lcd.clear();
  lcd.print("Number is");
}

void loop(){
  lcd.setCursor(0,1);
  lcd.print(String(testNum));
  testNum = testNum + 3;
}

I use the LCD instead of serial to debug. Good enough I guess

Why "long long"?
a range of -9223372036854775808 to +9223372036854775807 is fairly rarely required.
Even a "long" is going to take about 20 minutes to rollover (estimate/guess).