简体   繁体   English

32bit int * 32bit int = 64 bit int?

[英]32bit int * 32bit int = 64 bit int?

In other words does this work as expected? 换句话说,这是否按预期工作?

int32 i = INT_MAX-1;
int64 j = i * i;

or do I need to cast the i to 64 bit first? 或者我需要先将i转换为64位?

You need to cast at least one of the operands to the multiply. 您需要将至少一个操作数强制转换为乘法。 At the point the multiply is being done, the system doesn't know you're planning to assign to an int64. 在乘法完成时,系统不知道您计划分配给int64。

(Unless int64 is actually the native int type for your particular system, which seems unlikely) (除非int64实际上是您的特定系统的本机int类型,这似乎不太可能)

It depends on what int32 and int64 are. 这取决于int32和int64是什么。

In brief, all integers are promoted to at least 'int' size (which may be 64 bits) before any arithmetic operations, and to the size of the larger operand for binary operators if this is of greater rank than an int. 简而言之,在任何算术运算之前,所有整数都被提升为至少'int'大小(可能是64位),如果它的排名大于int,则为二进制运算符的较大操作数的大小。

How the result of an expression is used (whether or not it is stored to a wider type) has no bearing on the promotions of the constituent parts of the expression. 如何使用表达式的结果(无论它是否存储到更宽的类型)与表达式的组成部分的促销无关。

The basic answer is no it will not do what you want. 基本的答案是不,它不会做你想要的。
But it does do what is expected. 但它确实做了预期的事情。

Two things to note about mathematical operations: 有关数学运算的两点注意事项:

  • Both operands will be the same type. 两个操作数都是相同的类型。
  • The resulting type will be the same as the input. 结果类型将与输入相同。

If the compiler notes a mismatch between the operands it will convert one of the operands so that both match (see Which variables should I typecast when doing math operations in C/C++? ). 如果编译器注意到操作数之间不匹配,它将转换其中一个操作数,以便两者匹配(请参阅在C / C ++中进行数学运算时我应该对哪些变量进行类型转换? )。 Note: This is done in isolation to what happens to the result. 注意:这是根据结果发生的事情单独完成的。

Given two numbers a,b and each number uses len_a and len_b bits. 给定两个数字a,b,每个数字使用len_a和len_b位。

Your output datatype needs at least: len_a and len_b bits. 您的输出数据类型至少需要:len_a和len_b位。

In your above code, you have two 31 bit numbers ( because INT_MAX - 1 = 0x7FFFFFFE uses 31 bits ) and you will need to typecast one of them to int64_t because it will do a 32 bit multiply and overflow before it casts to int64_t. 在上面的代码中,您有两个31位数字(因为INT_MAX - 1 = 0x7FFFFFFE使用31位)并且您需要将其中一个类型转换为int64_t,因为它将在转换为int64_t之前执行32位乘法和溢出。


The number of bits needed for fixed point multiplication: 固定点乘法所需的位数:

len_output = howManyBits( a * b )
           = len_a + len_b

A quick example to show the above rule in action: 显示上述规则的快速示例:

a     = 7
len_a = 3

b     = 7
len_b = 3

c = a * b
  = 49 ( decimal )
  = 0x31 ( hex )

len_c = howManyBits( 0x31 ) 
      = 6

You can write a function to count bits. 您可以编写一个计数位的函数。 Or if you just want a quick sanity check to confirm this use something like Windows Calc that will convert the number into binary form and count the bits used. 或者,如果您只是想要快速进行健全性检查以确认此操作,请使用类似Windows Calc的内容,将数字转换为二进制形式并计算使用的位数。

See: 14. Mixed use of simple integer types and memsize types. 请参阅:14。简单整数类型和memsize类型的混合使用。 http://www.viva64.com/art-1-2-599168895.html http://www.viva64.com/art-1-2-599168895.html

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM