Arduino Arithmetic error negative result

Rickstar

I am trying to times 52 by 1000 and i am getting a negative result

int getNewSum = 52 * 1000; 

but the following code is ouputting a negative result: -13536

Matt Gibson

An explanation of how two's complement representation works is probably better given on Wikipedia and other places than here. What I'll do here is to take you through the workings of your exact example.

The int type on your Arduino is represented using sixteen bits, in a two's complement representation (bear in mind that other Arduinos use 32 bits for it, but yours is using 16.) This means that both positive and negative numbers can be stored in those 16 bits, and if the leftmost bit is set, the number is considered negative.

What's happening is that you're overflowing the number of bits you can use to store a positive number, and (accidentally, as far as you're concerned) setting the sign bit, thus indicating that the number is negative.

In 16 bits on your Arduino, decimal 52 would be represented in binary as:

0000 0000 0011 0100

(2^5 + 2^4 + 2^2 = 52)

However, the result of multiplying 52 by 1,000 -- 52,000 -- will end up overflowing the magnitude bits of an int, putting a '1' in the sign bit on the end:

*----This is the sign bit. It's now 1, so the number is considered negative.
1100 1011 0010 0000

(typically, computer integer arithmetic and associated programming languages don't protect you against doing things like this, for a variety of reasons, mostly related to efficiency, and mostly now historical.)

Because that sign bit on the left-hand end is set, to convert that number back into decimal from its assumed two's complement representation, we assume it's a negative number, and then first take the one's complement (flipping all the bits):

0011 0100 1101 1111

-- which represents 13,535 -- and add one to it, yielding 13,536, and call it negative: -13,536, the value you're seeing.

If you read up on two's complement/integer representations in general, you'll get the hang of it.

In the meantime, this probably means you should be looking for a bigger type to store your number. Arduino has unsigned integers, and a long type, which will use four bytes to store your numbers, giving you a range from -2,147,483,648 to 2,147,483,647. If that's enough for you, you should probably just switch to use long instead of int.

本文收集自互联网,转载请注明来源。

如有侵权,请联系[email protected] 删除。

编辑于
0

我来说两句

0条评论
登录后参与评论

相关文章

来自分类Dev

Fatal error: Cannot use isset() on the result of an expression

来自分类Dev

Rust Diesel原始SQL给出错误“ std :: result :: Result <Vec <T>,柴油机:: result :: Error>`所需的类型注释”

来自分类Dev

没有为`std :: result :: Result <reqwest :: Response,reqwest :: Error>`实现特性std :: future :: Future`

来自分类Dev

Unable to generate a temporary class (result=1). error CS0030:

来自分类Dev

Arduino电源

来自分类Dev

Arduino Simulink

来自分类Dev

arduino 读写

来自分类Dev

Arithmetic Shift Not Working

来自分类Dev

Regular expression for arithmetic expression

来自分类Dev

Attempting to understand pointer arithmetic

来自分类Dev

如何在Google Apps脚本中修复{“ result”:“ error”,“ error”:{“ name”:“ Exception”}}

来自分类Dev

通讯Arduino-C ++不读取Arduino

来自分类Dev

通讯Arduino-C ++不读取Arduino

来自分类Dev

DRY arithmetic expression evaluation in Prolog

来自分类Dev

"Attempt to read a row while there is no result set associated with the statement" on Linux server but no error on Windows computer

来自分类Dev

我如何从Serde返回一个返回Result <(),Error>的函数中的错误

来自分类Dev

在API请求中实现Swift Result <Success,Error>时遇到问题

来自分类Dev

Negative numeric argument in Vim

来自分类Dev

HCF of negative numbers

来自分类Dev

ROS和arduino的接口

来自分类Dev

Arduino:上载超时错误

来自分类Dev

编译Arduino时出错

来自分类Dev

带字符的Arduino开关

来自分类Dev

Arduino Nano上的WiFi

来自分类Dev

Arduino Android蓝牙通信

来自分类Dev

Android + Arduino蓝牙通讯

来自分类Dev

arduino上的循环变量

来自分类Dev

Arduino:if-compare参数

来自分类Dev

将Arduino与Qt连接

Related 相关文章

热门标签

归档