【发布时间】:2017-03-02 19:38:24
【问题描述】:
我正在尝试将 8 字节宽的数字转换为位字符串。
#include <stdio.h>
#include <stdlib.h>
#include <string>
#include <bitset>
void getAsString(int64_t mask )
{
printf("mask = %lld\n", mask);
std::string test = std::bitset< 64 >( mask ).to_string();
printf("test = %s \n", test.c_str() );
}
int main()
{
int64_t allowedBits[4] = { 0x0000000000000001L, 0x0000000000010000L, 0x0000000100000000L, 0x0001000000000000L };
for (int i = 0; i < 4; ++i)
{
getAsString(allowedBits[i]);
}
}
下面是Linux机器上的输出
没有可用的 LSB 模块。 经销商编号:Ubuntu 说明:Ubuntu 16.04 LTS 发布时间:16.04 代号:xenial g++ (Ubuntu 5.4.0-6ubuntu1~16.04.4) 5.4.0 20160609
mask = 1
test = 0000000000000000000000000000000000000000000000000000000000000001
mask = 65536
test = 0000000000000000000000000000000000000000000000010000000000000000
mask = 4294967296
test = 0000000000000000000000000000000100000000000000000000000000000000
mask = 281474976710656
test = 0000000000000001000000000000000000000000000000000000000000000000
这台机器的代码相同。 发行商编号:CentOS 说明:CentOS 6.8 版(最终版) 发布:6.8 代号:Final
g++ (GCC) 4.4.7 20120313(红帽 4.4.7-17)
给出下面的输出
mask = 1
test = 0000000000000000000000000000000000000000000000000000000000000001
mask = 65536
test = 0000000000000000000000000000000000000000000000010000000000000000
mask = 4294967296
test = 0000000000000000000000000000000000000000000000000000000000000000
mask = 281474976710656
test = 0000000000000000000000000000000000000000000000000000000000000000
我检查了 int64_t 的宽度。两台机器都是8。 对于掩码 4294967296 和 281474976710656,字符串为零。
这里的任何帮助将不胜感激。
【问题讨论】:
-
如果
int64_t不是 8 个字节,你就有问题了。 -
mask的类型是什么?提示:这意味着提供minimal reproducible example -
按预期工作here。
-
还没有完成。 (完整应包含类似“
int main() {”和“}”的内容。) -
@NiyazK 请将其编辑到问题中。作为评论无法阅读。