Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/150.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/r/84.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ 布尔数到64位整数的字符数组_C++ - Fatal编程技术网

C++ 布尔数到64位整数的字符数组

C++ 布尔数到64位整数的字符数组,c++,C++,我有一个字符数组(比如char-charr[5]),它包含0/1(布尔数的字符数组)。现在,我想将字符数组转换为64位整数(如果数组是{0,0,0,1,0},它将给出2)。怎么做?有库函数吗?没有,没有标准函数。但这很简单: uint64_t pack(const uint8_t *bits, size_t n) { uint64_t x = 0, value = 1 << (n - 1); while(n > 0) { x += value * *bit

我有一个字符数组(比如char-charr[5]),它包含0/1(布尔数的字符数组)。现在,我想将字符数组转换为64位整数(如果数组是{0,0,0,1,0},它将给出2)。怎么做?有库函数吗?

没有,没有标准函数。但这很简单:

uint64_t pack(const uint8_t *bits, size_t n)
{
  uint64_t x = 0, value = 1 << (n - 1);

  while(n > 0)
  {
    x += value * *bits++;
    n--;
    value /= 2;
  }
  return x;
}
uint64\u t块(常量uint8\u t*位,大小\u t n)
{
uint64_t x=0,值=10)
{
x+=值**位++;
n--;
数值/=2;
}
返回x;
}

不,没有标准的功能。但这很简单:

uint64_t pack(const uint8_t *bits, size_t n)
{
  uint64_t x = 0, value = 1 << (n - 1);

  while(n > 0)
  {
    x += value * *bits++;
    n--;
    value /= 2;
  }
  return x;
}
uint64\u t块(常量uint8\u t*位,大小\u t n)
{
uint64_t x=0,值=10)
{
x+=值**位++;
n--;
数值/=2;
}
返回x;
}
尝试使用base 2:

int val = strtoll(input, NULL, 2);
尝试使用base 2:

int val = strtoll(input, NULL, 2);

Unwind的基本思想是正确的,但实现起来很复杂。这也适用于:

uint64_t pack(const uint8_t *bits, size_t n)
{
  uint64 x = 0;
  for(;n > 0; n--) // For all input bits.
  {
    x <<= 1; // make room for next bit.
    assert(*bits <= 1); // It better be a 0 or 1.
    x += *bits++; // Add new bit on the end.
  }
  return x;
}
uint64\u t块(常量uint8\u t*位,大小\u t n)
{
uint64 x=0;
for(;n>0;n--)//用于所有输入位。
{

xUnwind的基本思想是正确的,但它的实现非常复杂。这也适用于:

uint64_t pack(const uint8_t *bits, size_t n)
{
  uint64 x = 0;
  for(;n > 0; n--) // For all input bits.
  {
    x <<= 1; // make room for next bit.
    assert(*bits <= 1); // It better be a 0 or 1.
    x += *bits++; // Add new bit on the end.
  }
  return x;
}
uint64\u t块(常量uint8\u t*位,大小\u t n)
{
uint64 x=0;
for(;n>0;n--)//用于所有输入位。
{

只需手工操作…使用位运算符解决这个问题并不特别困难。如果它确实是一个小数组,并且您经常需要它,我将构建一个查找表,只需手工操作…使用位运算符解决这个问题并不特别困难。如果它确实是一个小数组,并且您经常需要它,我将构建一个lo好的,上表吧,不,这将需要
'0'
'1'
,即数字,这不是输入所具有的。@unwind:但这是可修复的。只需将
'0'
添加到所有数字中即可。(如果您有
字符[]
,而不是如果您有
常量字符*
,在这种情况下,您需要先制作一份副本)嗯,不,这将需要
'0'
'1'
,即数字,这不是输入所具有的。@unwind:但这是可修复的。只需将
'0'
添加到所有数字中即可。(如果您有
字符[]
,而不是如果您有
常量字符*
,在这种情况下,您需要先制作一份副本)我会去掉
value
,只使用移位,否则很好。我会去掉
value
,只使用移位,否则很好。