Weird Quirk with ITOA
alexanbo
Posts: 282
Was doing some troubleshooting on a CRC calculation and noticed that when I used ITOA to show some data it gave me some odd results
Returned:
Test 1:16776960
Test 2:0
Which is odd because I expected Test 1 to be $FF00 not $FFFF00. In digging I found that ITOA's parameter is actually a Long which kinda explains the first result, but then it's not consistent with the second result?
button_event[10128:1:0,1] { push: { stack_var integer crc crc = $ffff send_string 0,"'Test 1:',itoa(crc << 8)" send_string 0,"'Test 2:',itoa(crc + 1)" } }
Returned:
Test 1:16776960
Test 2:0
Which is odd because I expected Test 1 to be $FF00 not $FFFF00. In digging I found that ITOA's parameter is actually a Long which kinda explains the first result, but then it's not consistent with the second result?
0
Comments
send_string 0,"'Test 1:',itoa(crc << 8)"
To this equivalent:
send_string 0,"'Test 1:',itoa(crc * 256)"
Then you get the expected result of 65280 ($FF00)
Weird indeed.
For example if you added a line before the print statement
crc = $ffff << 8
and changed the print statement to:
send_string 0,"'Test1:',itoa(crc)"
it would print out the $ff00 decimal equivalent
Yes, because crc is an integer (16 bits).
If you said
then the result is $ffff00, because test is a 32-bit integer.
It's nothing to do with itoa aside from the fact that its argument is a LONG_INTEGER. The $ffff+1 test may be running into some weird signed/unsigned arithmetic as well.