考虑这个功能:
unsigned hash(char *s)
{
char *p;
unsigned hashval;
for(p = s; *p; p++)
hashval = *p + 31 * hashval;
return hashval;
}
Run Code Online (Sandbox Code Playgroud)
如何测量s将返回错误结果的字节数,例如溢出?我在32位平台上.
我有一个使用该数字的python代码2637268776(大于sys.maxint32位系统).因此它保存为long类型.
我在我的代码中使用了C++框架绑定,因此我有一个案例,它被转换为int32,导致int32溢出:
2637268776 --> -1657698520
Run Code Online (Sandbox Code Playgroud)
在我的情况下,它只能发生一次,因此可以安全地假设如果整数是负数,我们只有一个int溢出.我怎样才能在数学上反转数字?
我试图在c中创建一个程序来解决数独游戏,其中我试图在无符号长整数变量中存储一个等于2*3*5*7*11*13*17*19*23*29的数字.在32位Ubuntu机器上使用boath gcc和g ++进行编译时,我收到整数溢出错误.
请帮我存储和使用这个号码,或建议替代方案.
我将整个代码包括在内以供参考.
#include <stdio.h>
#include <stdlib.h>
int main() {
int sudoku[9][9] = {2, 8, 0, 0, 0, 3, 0, 0, 0, // 0 is used to denote blank
4, 7, 0, 0, 8, 6, 0, 9, 1, 0, 0, 5, 0, 9, 0, 2, 3, 0,
0, 9, 0, 5, 2, 0, 4, 8, 6, 5, 0, 0, 0, 0, 0, 0, 0, 3,
8, 6, 1, 0, 7, 4, 0, 5, 0, 0, 3, 4, 0, …Run Code Online (Sandbox Code Playgroud) 我被要求编写一个带整数的Java函数n,并打印出该值n^100.
我不知道如何处理这个问题.我知道通过常规手段它会随着n增长而溢出.答案如:5.32 x 10^20不可接受.它必须是每一个数字.
所以,例如:
public void byHundred(int n) {
result = //some computation that yields the string
System.out.println(result);
}
Run Code Online (Sandbox Code Playgroud)
所以,byHundred(23)打印出来"14886191506363039393791556586559754231987119653801368686576988209222433278539331352152390143277346804233476592179447310859520222529876001"
我有一个带有简短变量声明的简单程序:
short int v=0XFFFD;
printf("v = %d\n",v);
printf("v = %o\n",v);
printf("v = %X\n",v);
Run Code Online (Sandbox Code Playgroud)
结果是:
v = -3 ; v = 37777777775 ; v = FFFFFFFD
Run Code Online (Sandbox Code Playgroud)
我不明白如何计算这些值。我知道一个短变量可以保存 -32768 和 32767 之间的值,而值 0XFFFD 会导致溢出,但我不知道如何计算确切值,在这种情况下为 -3。
另外,如果我的声明是 v=0XFFFD 为什么输出 v=%X 是 FFFFFFFD?
我正在使用以下代码在进程中获取唯一 ID:
for i := 0; i < 10; i++ {
go func() {
for {
atomic.AddUint32(&counter, 1)
time.Sleep(time.Millisecond)
}
}()
}
Run Code Online (Sandbox Code Playgroud)
如果计数器值超出 uint32 的限制会发生什么?
我正在尝试检查签名添加是否会溢出.一般来说,检查是否
int a + int b
Run Code Online (Sandbox Code Playgroud)
会溢出(a和b都是正面的),我检查是否
if (a > INT_MAX - b)
Run Code Online (Sandbox Code Playgroud)
但现在我想检查一下
int a + int b - int c
Run Code Online (Sandbox Code Playgroud)
会溢出来的.我知道a,b和c是正数,b> = c,所以我做了以下检查:
if (a > INT_MAX - b + c)
Run Code Online (Sandbox Code Playgroud)
现在我的问题是,编译器可以重写
INT_MAX - b + c to INT_MAX + c - b ?
Run Code Online (Sandbox Code Playgroud)
我担心的是,它会首先执行INT_MAX + c,这可能会溢出并导致未定义的行为.
我有一些代码检查整数是否在[?2 ^ 31 + 1,2 ^ 31?1]。但是,在编译期间,将引发整数溢出警告。
long int tmp_l = strtol(tokens[8].c_str(),NULL,10);
if (tmp_l >= ( (int32_t)-1 * ( ((int32_t)1<<31) - (int32_t)1) ) &&
tmp_l <= ( ((int32_t)1 << 31) - (int32_t)1) ) {
long int in_range = tmp_l;
} else {
cerr << "ERROR: int not in range. Expected [(-2^31)-1, (2^31)-1]. ";
cerr << "Found: " << tmp_l << endl;
}
Run Code Online (Sandbox Code Playgroud)
main.cpp:93:51: warning: integer overflow in expression [-Woverflow]
if (tmp_l >= ((int32_t)-1 * (((int32_t)1<<31) - (int32_t)1) ) &&
~~~~~~~~~~~~~~~~~^~~~~~~~~~~~ …Run Code Online (Sandbox Code Playgroud) Java使用32位作为字符提示-因此最大值为65536。
但是以下代码为我提供了标题中报告的结果。
public static void main(String[] args) {
int a = 10000000;
char b = 33;
b = (char)a;
System.out.println((int)b);
}
Run Code Online (Sandbox Code Playgroud) 因此,我决定测试一个新的 C++ 海龟式图形库。我决定通过制作斐波那契螺旋来测试它。但是,运行该程序后,它会在中间停止。我检查了发生了什么,斐波那契值已经反转为-2147483647,就好像它是用32位编译的一样。有人可以帮忙吗?我在 Windows 上用 g++ 编译了它。注意:我使用向量来存储值。这是我的代码:
//Includes and namespaces
#include "Cturtle.hpp"
#include<iostream>
namespace ct = cturtle;
std::vector<long int> fibonacci = {1, 2}; //This vector stores the fib sequence
ct::TurtleScreen scr; //Create a new Screen
ct::Turtle turtle(scr); //Create a new turtle on the screen
int main() {
turtle.speed(ct::TS_FASTEST); //Set the turtle's speed to max
while(true) {
for(int i = 0; i < fibonacci.back(); i++) {
fibonacci.push_back(fibonacci.back() + fibonacci.back()-1); //Add a new value to the fibonacci vector based off the previous …Run Code Online (Sandbox Code Playgroud)