如何输入 int64_t/uint64_t 常量? [英] How to input int64_t / uint64_t constants?

查看:53
本文介绍了如何输入 int64_t/uint64_t 常量?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想要做的是定义一个等于 2^30 的常量(我可能会将其更改为 2^34 之类的值,因此我更喜欢为它设置一个大于 32 位的房间).

What I'm trying to do is to define a constant equal to 2^30 (I may change it to something like 2^34, so I prefer to have a room larger than 32 bits for it).

为什么下面的最小(?)示例无法编译?

Why the following minimal(?) example doesn't compile?

#include <stdint.h>
// test.cpp:4:33: error: expected primary-expression before numeric constant
// test.cpp:4:33: error: expected ')' before numeric constant
const uint64_t test = (uint64_t 1) << 30;
//const uint64_t test1 = (uint64_t(1)) << 30;// this one magically compiles! why?

int main() { return 0; }

推荐答案

(uint64_t 1) 是无效的语法.转换时,您可以使用 uint64_t(1)(uint64_t) 1.注释掉的示例之所以有效,是因为它遵循正确的强制转换语法,就像这样:

(uint64_t 1) is not valid syntax. When casting, you can either use uint64_t(1) or (uint64_t) 1. The commented out example works because it follows the proper syntax for casting, as would:

const uint64_t test = ((uint64_t)1) << 30;

虽然这直接回答了问题,但请参阅 Shafik Yaghmour 的回答,了解如何正确定义积分常数具体尺寸.

While this directly answers the question, see the answer by Shafik Yaghmour on how to properly define an integral constant with specific size.

这篇关于如何输入 int64_t/uint64_t 常量?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆