标签: integer-overflow

具有32位寄存器的双精度整数减法(MIPS)

我正在学习计算机算术.我使用的书(Patterson和Hennessey)列出了以下问题.

写mips代码以对64位数据进行双精度整数减法.假设第一个操作数在寄存器$ t4(hi)和$ t5(lo)中,第二个在$ t6(hi)和$ t7(lo)中.

我对答案的解决方案是

sub  $t3, $t5, $t7   # Subtract lo parts of operands. t3 = t5 - t7
sltu $t2, $t5, $t7   # If the lo part of the 1st operand is less than the 2nd,
                     #   it means a borrow must be made from the hi part
add  $t6, $t6, $t2   # Simulate the borrow of the msb-of-low from lsb-of-high
sub  $t2, $t4, $t6   # Subtract the hi's. t2 = t4 - t6
Run Code Online (Sandbox Code Playgroud)

然而,作者给出了针对该问题的解决方案如下

对于有符号的双精度整数, …

unsigned signed mips integer-overflow double-precision

2
推荐指数
1
解决办法
5069
查看次数

将十六进制字符串转换为long

我试图在32位机器上进行十六进制到整数转换.这是我正在测试的代码,

int main(int argc,char **argv)
{
    char *hexstring = "0xffff1234";
    long int n;

    fprintf(stdout, "Conversion results of string: %s\n", hexstring);
    n = strtol(hexstring, (char**)0, 0); /* same as base = 16 */
    fprintf(stdout, "strtol = %ld\n", n);
    n = sscanf(hexstring, "%x", &n);
    fprintf(stdout, "sscanf = %ld\n", n);
    n = atol(hexstring);
    fprintf(stdout, "atol = %ld\n", n);
    fgetc(stdin);

    return 0;
  }
Run Code Online (Sandbox Code Playgroud)

这就是我得到的:

 strtol = 2147483647 /* = 0x7fffffff -> overflow!! */
 sscanf = 1 /* nevermind */
 atol = 0   /* nevermind …
Run Code Online (Sandbox Code Playgroud)

c hex integer-overflow type-conversion strtol

2
推荐指数
1
解决办法
7180
查看次数

Java Hashcode提供整数溢出

背景资料:

在我的项目中,我正在将强化学习(RL)应用于Mario域.对于我的状态表示,我选择使用带有自定义对象的哈希表作为键.我的自定义对象是不可变的,并且覆盖了.equals()和.hashcode()(由IntelliJ IDE生成).

这是生成的.hashcode(),我在注释中添加了可能的值作为额外信息:

@Override
public int hashCode() {
    int result = (stuck ? 1 : 0);                // 2 possible values: 0, 1
    result = 31 * result + (facing ? 1 : 0);     // 2 possible values: 0, 1 
    result = 31 * result + marioMode;            // 3 possible values: 0, 1, 2
    result = 31 * result + (onGround ? 1 : 0);   // 2 possible values: 0, 1 
    result = 31 * result + (canJump ? 1 …
Run Code Online (Sandbox Code Playgroud)

java hash integer-overflow hashcode integer-hashing

2
推荐指数
2
解决办法
2413
查看次数

电话号码改为2147483647?如何表示电话号码?

因此,我创建了一个数据表,要求提供用户移动提供商 (varchar)、电话号码和密码。该表似乎运行良好,除了当用户输入电话号码时原始号码转换为 2147483647。我花了一些时间环顾四周,我了解到这是因为我已经超出了表的 32 位限制。为了解决这个问题,我需要在 MYSQL 数据库中使我的整数限制大于 10(我原来拥有的)。

我已经使用 PhpMyAdmin 更改了 MYSQL 表;但是,我仍然遇到同样的问题。这是因为我现在需要在 Yii 端更改某些内容吗?

我知道我可以使用 CRUD 来构建新的 MVC 关系 - 但我已经向模型和控制器添加了相当多的功能。Yii 有没有办法以零碎的方式更改我的数据库?即无需使用 Crud?

php mysql telephony integer-overflow

2
推荐指数
1
解决办法
2694
查看次数

Java 8无符号整数加法和潜在溢出

我正在使用Cay S. Horstmann的书"Java SE 8 for the Really Impatient"进行练习.基于Number课程改进的练习之一是:

编写一个程序,使用值和无符号运算对0到2 32 - 1 之间的数字进行相加,减法,除法和比较int.说明原因divideUnsigned并且remainderUnsigned是必要的.

问题是,如果添加2个无符号整数,则总和可能溢出整数限制.我没有看到一种方法来防止这种情况,如果不使用long来存储总和并检查它是否大于Integer.MAX_VALUE.是否可以仅使用整数执行此操作?

numbers integer-overflow addition unsigned-integer java-8

2
推荐指数
1
解决办法
1627
查看次数

突然无限循环高于某些输入参数?

在学习Java的同时,我正在重做一些Project Euler问题.这是关于问题14 - 最长的Collat​​z序列:https://projecteuler.net/problem=14

我的程序运行得很好,比较低,CEILING比如1000,但是当像post一样执行它无限循环时,我想?这里出了什么问题?

public class Test {
    public static void main(String[] args) {
        int tempMax = 0;
        final int CEILING = 1_000_000;

        for (int j = 1; j < CEILING; ++j) {
            tempMax = Math.max(tempMax, collatzLength(j));
        }
        System.out.println(tempMax);
    }

    static int collatzLength(int n) { //computes length of collatz-sequence starting with n
            int temp = n;

            for (int length = 1; ; ++length) {
                if (temp == 1) 
                    return length;
                else if (temp % …
Run Code Online (Sandbox Code Playgroud)

java integer-overflow infinite-loop

2
推荐指数
1
解决办法
68
查看次数

如何在Swift数组中存储多长时间?

我有一些我想要在数组中发送的用户ID,但是无法找出用于创建具有非常长整数的数组的正确Swift 3语法.我尝试了转换,@前缀和使用as AnyObject,但这不起作用.

let idArray = [10211420262370680, 10211420262370680]
Run Code Online (Sandbox Code Playgroud)

错误:存储到int时整数文字溢出

创建具有如此长整数的数组的正确方法是什么?

arrays integer-overflow ios long-integer swift

2
推荐指数
2
解决办法
1550
查看次数

无论输入如何,为什么大型计算循环会产生相同的输出?

我做了这些随机操作的大循环来进行基准测试(仅仅因为我很好奇)并遇到了一些我不明白的事情.无论我为大循环输入什么,它总是产生相同的结果.这是我正在讨论的部分的代码.

public int stuff;
public int result;

myJavaProgram(String[] cmdArguments)
{ 
  stuff=1;
  superLoopCalcualtion();
  System.out.println(stuff+" converted to result:"+result); 

  stuff=12345;
  superLoopCalcualtion();
  System.out.println(stuff+" converted to result:"+result); 

  stuff=9823450;
  superLoopCalcualtion();
  System.out.println(stuff+" converted to result:"+result); 
}  

public void superLoopCalcualtion()
{
  int a=stuff;
  int b=a+99;   
  int z=0;
  for (z = 0; z < 200000; z++) 
  {
    a=a+22;
    b=a*44;
    a=b+1234;
  }
  result=a;
} 
Run Code Online (Sandbox Code Playgroud)

输出就是这个

1 converted to result:1398361394
12345 converted to result:1398361394
9823450 converted to result:1398361394
Run Code Online (Sandbox Code Playgroud)

没有办法可以正确......对吧?

java for-loop integer-overflow

2
推荐指数
1
解决办法
89
查看次数

INT_MAX +( - 1)是未定义的行为吗?

在C++中,签名类型的溢出是未定义的行为.以下示例是否也是未定义的行为?

#include <limits.h>

int f() {
  int a = INT_MAX;
  int b = -1;
  return a + b;
}
Run Code Online (Sandbox Code Playgroud)

它不是数学上下文中的溢出,但CPU可能会看到它 add 0x7fffffff 0xffffffff.

c++ integer-overflow undefined-behavior

2
推荐指数
2
解决办法
431
查看次数

剩余时间测量的 std::chrono::duration 溢出

我想知道,还有多少时间让事件发生。我为此使用 boost::chrono 或 std::chrono 。

基本上算法是这样的:

using std::chrono; // Just for this example
auto start = steady_clock::now();
seconds myTotalTime(10);
while(true){
  auto remaining = start + myTotalTime - steady_clock::now();
  seconds remainingSecs = duration_cast<seconds>(remaining);
  if(remainingSecs.count() <= 0) return true;
  else print("Remaining time = " + remainingSecs.count()); // Pseudo-Code!
}
Run Code Online (Sandbox Code Playgroud)

现在(理论上)可能会发生以下情况:start接近时钟周期的末尾(IMO 不知道这0意味着什么,所以它可能是任意的)。
然后start + myTotalTime可能溢出,减法可能下溢。

即使是简单的事情,如steady_clock::now() - start可能下溢。

对于无符号类型,这不是问题。如果它们是无符号的,那么标准保证steady_clock::now() - start在溢出now()和下溢减法的情况下我仍然得到正确数量的“单位” :10 - 250 = 10 + 256 - 255 …

c++ integer-overflow c++11 c++-chrono

2
推荐指数
1
解决办法
1633
查看次数