其实我(可能)是一个"简单"的问题.所以我不知道如何将有符号整数转换为无符号整数.
我的代码:
signed int entry = 0;
printf("Decimal Number : ");
scanf("%d", &entry);
unsigned int uEntry= (unsigned int) entry;
printf("Unsigned : %d\n", uEntry);
Run Code Online (Sandbox Code Playgroud)
如果我将无符号值发送到控制台(参见我的上一个代码行),我总是得到一个有符号整数.
你能帮助我吗?
非常感谢!
亲切的问候,亲
为什么f()下面的代码中的第一个调用最终会打印出-2我传递给它的代码4294967294呢?
#include <stdio.h>
#include <stdint.h>
#include <stdarg.h>
#include <stdlib.h>
void f(int64_t i, ...)
{
va_list ap;
int64_t j;
va_start(ap, i);
j = va_arg(ap, int64_t);
printf("%jd %jd\n", (intmax_t) i, (intmax_t) j);
va_end(ap);
}
int main()
{
f(-1, -2); // Prints -1 4294967294 (bug!)
f(-1, (int64_t) -2); // Prints -1 -2 (fix!)
return 0;
}
Run Code Online (Sandbox Code Playgroud)
我能理解为什么f()修复的第二次调用有效.但我无法理解为什么第一个f()电话会导致这个问题.你能解释一下这种行为吗?
c signed variadic-functions integer-promotion unsigned-integer
我正在构建一个八叉树数据结构,为了节省最终节点的内存,我希望将值直接存储在指针中,而不是必须创建一个容纳8个子节点的对象.
我的数据类型是uint32_t,这意味着指针有足够的位来将其保存在x86或amd64上.
那么如何在x86或amd64指针中存储无符号32位整数?
伪代码:
uint32_t i = 123;
Octree* ptr = i;
uint32_t ii = ptr;
std::cout << ii << std::endl; //Prints 123
Run Code Online (Sandbox Code Playgroud)
这怎么可能?
在这个主函数中,我收到一条错误消息,指出我不应该使用 %hu(无符号短整型的格式说明符),并且无符号短整型的类型似乎是无符号长整型。怎么会这样?
#include <stdio.h>
int main (void) {
unsigned short int a, b, c;
printf("sizeof short int - %hu bytes \n\n", sizeof(unsigned short int));
}
Run Code Online (Sandbox Code Playgroud)
外壳输出是:
warning: format specifies type
'unsigned short' but the argument has type
'unsigned long' [-Wformat]
...%hu bytes \n\n", sizeof(unsigned short int));
~~~ ^~~~~~~~~~~~~~~~~~~~~~~~~~
%lu
Run Code Online (Sandbox Code Playgroud) 我正在尝试在 Java 中将 double 转换为 unsigned int。
使用 unsigned int 我的意思是从 0 到 4294967295 的范围。
我int用作载体 - 它可以转换为带有Integer.toUnsignedString.
我不在乎我会得到什么结果
NaN。Inf或-Inf< 0或> 4294967295。只要我得到任何结果。
简单的演员表不起作用:
(int) 4294967295d == 2147483647
(int) 2147483648d == 2147483647
Run Code Online (Sandbox Code Playgroud)
所以有问题的范围是2147483648d- 4294967295d。
有没有一种快速的方法可以正确覆盖该范围?
tl;dr:在 CI 中会使用(unsigned int) dblValue- 我会在 Java 中使用什么?
我知道使用(访问值)类型为内置整数类型的未初始化的非静态和非全局对象是未定义的行为。
int x; // x is defined inside a function scope for example main
++x;// UB
signed char c = 127; // max positive value for char on my machine
c++; // UB overflowing a signed char.
Run Code Online (Sandbox Code Playgroud)
到这里还可以,但是无符号类型呢?
unsigned char uc = 255; // ok
uc++; // ok c now has the value 0
Run Code Online (Sandbox Code Playgroud)
在这里没问题,因为溢出无符号将丢弃范围之外的位。
因此,只要分配给无符号的任何值都是无害的:
unsigned int x; // local non-static uninitialized
std::cout << x << '\n';// is it UB or OK?
Run Code Online (Sandbox Code Playgroud)
正如您所看到的,设置为无符号的任何(不确定)值都不会导致 UB,所以这样做是错误的吗?
我不关心什么值,x但我认为它不会造成任何伤害。
如果没问题,那么我想编译器可以使用未初始化的无符号非静态非全局对象生成一个随机值。
我正在移植一些在 uint32_t 上进行取模的 C 代码。uint32_t 按位适合 Java int,但我无法弄清楚如何在不转换为 long 的情况下对其执行模运算。这是我现在的代码:
int i = 0xffffffff;
long asUnsigned = Integer.toUnsignedLong(i);
int mod = (int) (asUnsigned % 42L);
Run Code Online (Sandbox Code Playgroud)
我可以在不转换为长整型的情况下执行此模计算吗?
java bit-manipulation modulo unsigned-integer signed-integer
我有以下简单的程序,它读取以字符串形式给出的数字并打印它。它适用于小数字,但是当我尝试使用大小为 unsigned long 的数字(例如“18446744073709551615”)时,它不会产生预期的结果。这是我的代码:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
void readNumbers(char* numbers, unsigned long numbers_length, unsigned long* number)
{
char string[numbers_length + 1];
strncpy(string, numbers + 0, numbers_length);
*number = strtoul(string, NULL, 10);
}
int main () {
unsigned long number;
char* numbers = "18446744073709551615";
unsigned long numbers_length = strlen(numbers);
readNumbers(numbers, numbers_length, &number);
printf("X = %lu \n", number); // prints 4294967295
return 0;
}
Run Code Online (Sandbox Code Playgroud)
Edit_1:根据此站点, unsigned long 的最大值为 18446744073709551615。
Edit_2:以下代码适用于我的系统:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <limits.h> …Run Code Online (Sandbox Code Playgroud) 我是一名相当高级的程序员(有 Java、Python、C#、C、C++ 经验),现在第一次尝试学习 Rust。Rust 与我之前尝试过的任何语言都非常不同,所以我很挣扎。为了了解这门语言,我正在实现一个小游戏。
在我的代码中的某个时刻,我想访问“板”中的一个元素和周围的元素。如果我的元素位于板的边缘(索引 0 或索引最大值),我需要一个剪裁值(例如索引 0 - 1 == 0) 如果我简化这个用例,我会尝试从数组和相对索引。下面是一个非常小的例子:
fn get_elements(array: &Vec<char>, i: usize, dx: i8) -> (char, char) {
let center = array[i];
let other = array[i+dx];
(center, other)
}
fn main() {
let data = vec!['a', 'b', 'c', 'd', 'e'];
let (c, o) = get_elements(&data, 2, -1);
}
Run Code Online (Sandbox Code Playgroud)
正如预期的那样,这无法编译,因为no implementation for `usize + i8` . 我理解这是因为对于无符号数来说,减去零以下的行为是未定义的。然而,我不清楚如何在 Rust 中实现所需的行为。