Go Down

### Topic: 16-bit handles 32/64-bit weirdly (Read 3210 times)previous topic - next topic

#### akaJag

##### Feb 11, 2014, 09:01 pmLast Edit: Feb 11, 2014, 09:45 pm by akaJag Reason: 1
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).

#### Grumpy_Mike

#1
##### Feb 11, 2014, 09:09 pm
Quote
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

#### tack

#2
##### Feb 11, 2014, 09:10 pmLast Edit: Feb 11, 2014, 09:14 pm by tack Reason: 1
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. ;-)

#### akaJag

#3
##### Feb 11, 2014, 09:11 pm

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

#### akaJag

#4
##### Feb 11, 2014, 09:13 pmLast Edit: Feb 11, 2014, 09:15 pm by akaJag Reason: 1

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

#### tack

#5
##### Feb 11, 2014, 09:15 pmLast Edit: Feb 11, 2014, 09:22 pm by tack Reason: 1

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

Code: [Select]
`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.

#### akaJag

#6
##### Feb 11, 2014, 09:17 pm

Code: [Select]
`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.

#### tack

#7
##### Feb 11, 2014, 09:19 pmLast Edit: Feb 11, 2014, 09:21 pm by tack Reason: 1
Why did you show a Serial.print example in your first post then and quote certain outputs from the sketch you posted?

#### akaJag

#8
##### Feb 11, 2014, 09:21 pm

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!

#### tack

#9
##### Feb 11, 2014, 09:25 pm

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.

#### akaJag

#10
##### Feb 11, 2014, 09:28 pm

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.

#### akaJag

#11
##### Feb 11, 2014, 09:30 pm
With some nooby testing I just used String() and now it works. You do no longer need to spend time on this post

#### AWOL

#12
##### Feb 11, 2014, 09:30 pm
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?

Edit:
Quote
With some nooby testing I just used String()
Here we go...
"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.

#### akaJag

#13
##### Feb 11, 2014, 09:35 pm

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

Edit:
Quote
With some nooby testing I just used String()
Here we go...

And please don't look down on me like that

#### akaJag

#14
##### Feb 11, 2014, 09:43 pm
String() cannot convert long long to string, is there a alternative function I can use?

Go Up

Please enter a valid email to subscribe