P Pommie Well-Known Member Most Helpful Member Feb 16, 2023 #1 I know there's got to be a simple explanation but I can't see it, I thought all these would be the same, I'm trying to put 51 in the highest byte of work. Code: uint32_t work; work=51*(2^24); work=51<<24; work=51; work<<=24; but when the result is printed out, I get, 1326 0 855638016 The actual code, Code: uint32_t work; void setup(){ Serial.begin(115200); work=51*(2^24); Serial.println(work); work=51<<24; Serial.println(work); work=51; work<<=24; Serial.println(work); } void loop(){ } What am I missing? Thanks, Mike.
I know there's got to be a simple explanation but I can't see it, I thought all these would be the same, I'm trying to put 51 in the highest byte of work. Code: uint32_t work; work=51*(2^24); work=51<<24; work=51; work<<=24; but when the result is printed out, I get, 1326 0 855638016 The actual code, Code: uint32_t work; void setup(){ Serial.begin(115200); work=51*(2^24); Serial.println(work); work=51<<24; Serial.println(work); work=51; work<<=24; Serial.println(work); } void loop(){ } What am I missing? Thanks, Mike.
rjenkinsgb Well-Known Member Most Helpful Member Feb 16, 2023 #2 Try using 51L to force a long constant? Any intermediate math may be done as integer, otherwise.
P Pommie Well-Known Member Most Helpful Member Feb 16, 2023 #3 Yup, that fixed it except for the first one, even doing, work=51L*(2L^24L); And all other combos, Doesn't fix it - answer is 1326!!!! Mike.
Yup, that fixed it except for the first one, even doing, work=51L*(2L^24L); And all other combos, Doesn't fix it - answer is 1326!!!! Mike.
P Pommie Well-Known Member Most Helpful Member Feb 16, 2023 #5 rjenkinsgb said: What do you get for work=(2L^24L); Click to expand... Now, this is weird, I get 26. Mike.
rjenkinsgb said: What do you get for work=(2L^24L); Click to expand... Now, this is weird, I get 26. Mike.
P Pommie Well-Known Member Most Helpful Member Feb 16, 2023 #6 And also for, work=(2L^24); and work=(2^24); It appears that the compiler is taking ^ as +. Just tried, work=(2^20); and it gives 22!!! Mike.
And also for, work=(2L^24); and work=(2^24); It appears that the compiler is taking ^ as +. Just tried, work=(2^20); and it gives 22!!! Mike.
rjenkinsgb Well-Known Member Most Helpful Member Feb 17, 2023 #7 Looks like a compiler bug! It explains the 1326 result, at least. Does the same thing happen with small powers, eg. 2^3 ?
Looks like a compiler bug! It explains the 1326 result, at least. Does the same thing happen with small powers, eg. 2^3 ?
Ian Rogers User Extraordinaire Forum Supporter Most Helpful Member Feb 17, 2023 #8 ^ is a bitwise xor on the arduino ... (51<<24) is the way they do it or pow();
P Pommie Well-Known Member Most Helpful Member Feb 17, 2023 #9 Ian Rogers said: ^ is a bitwise xor on the arduino ... Click to expand... Knew I was missing something. Ian Rogers said: (51<<24) is the way they do it or pow(); Click to expand... Actually, that gives zero, As pointed out above, it should be, (51L<<24) Just kinda unlucky that 20 xor 2 is 22. Anyway, problem solved. Thanks all, Mike. Edit, note to self, stop using Excel terminology.
Ian Rogers said: ^ is a bitwise xor on the arduino ... Click to expand... Knew I was missing something. Ian Rogers said: (51<<24) is the way they do it or pow(); Click to expand... Actually, that gives zero, As pointed out above, it should be, (51L<<24) Just kinda unlucky that 20 xor 2 is 22. Anyway, problem solved. Thanks all, Mike. Edit, note to self, stop using Excel terminology.
Ian Rogers User Extraordinaire Forum Supporter Most Helpful Member Feb 17, 2023 #10 Yup! just checked.. constants are 16 bit unless u, l or ul are specified