将整数转换为位表示形式

发布于 2024-08-30 03:37:50 字数 104 浏览 7 评论 0原文

如何将整数转换为其位表示形式。我想获取一个整数并返回一个包含整数位表示形式的 1 和 0 的向量。

我自己尝试做这件事很费时间,所以我想我会问一下是否有内置的库函数可以提供帮助。

How can I convert a integer to its bit representation. I want to take an integer and return a vector that has contains 1's and 0's of the integer's bit representation.

I'm having a heck of a time trying to do this myself so I thought I would ask to see if there was a built in library function that could help.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(6

静若繁花 2024-09-06 03:37:51

不适用于底片。

vector<int> convert(int x) {
  vector<int> ret;
  while(x) {
    if (x&1)
      ret.push_back(1);
    else
      ret.push_back(0);
    x>>=1;  
  }
  reverse(ret.begin(),ret.end());
  return ret;
}

Doesn't work with negatives.

vector<int> convert(int x) {
  vector<int> ret;
  while(x) {
    if (x&1)
      ret.push_back(1);
    else
      ret.push_back(0);
    x>>=1;  
  }
  reverse(ret.begin(),ret.end());
  return ret;
}
故事与诗 2024-09-06 03:37:51

用一行代码解决这个问题并不太难,但实际上有一个标准库解决方案。

#include <bitset>
#include <algorithm>

std::vector< int > get_bits( unsigned long x ) {
    std::string chars( std::bitset< sizeof(long) * CHAR_BIT >( x )
        .to_string< char, std::char_traits<char>, std::allocator<char> >() );
    std::transform( chars.begin(), chars.end(),
        std::bind2nd( std::minus<char>(), '0' ) );
    return std::vector< int >( chars.begin(), chars.end() );
}

C++0x 甚至让它变得更容易!

#include <bitset>

std::vector< int > get_bits( unsigned long x ) {
    std::string chars( std::bitset< sizeof(long) * CHAR_BIT >( x )
        .to_string( char(0), char(1) ) );
    return std::vector< int >( chars.begin(), chars.end() );
}

这是图书馆最奇怪的角落之一。也许他们真正的目的是连载。

cout << bitset< 8 >( x ) << endl; // print 8 low-order bits of x

It's not too hard to solve with a one-liner, but there is actually a standard-library solution.

#include <bitset>
#include <algorithm>

std::vector< int > get_bits( unsigned long x ) {
    std::string chars( std::bitset< sizeof(long) * CHAR_BIT >( x )
        .to_string< char, std::char_traits<char>, std::allocator<char> >() );
    std::transform( chars.begin(), chars.end(),
        std::bind2nd( std::minus<char>(), '0' ) );
    return std::vector< int >( chars.begin(), chars.end() );
}

C++0x even makes it easier!

#include <bitset>

std::vector< int > get_bits( unsigned long x ) {
    std::string chars( std::bitset< sizeof(long) * CHAR_BIT >( x )
        .to_string( char(0), char(1) ) );
    return std::vector< int >( chars.begin(), chars.end() );
}

This is one of the more bizarre corners of the library. Perhaps really what they were driving at was serialization.

cout << bitset< 8 >( x ) << endl; // print 8 low-order bits of x
吖咩 2024-09-06 03:37:51

DCP答案的修改。该行为是为 t 的负值定义的实现。它提供所有位,甚至前导零。与使用 std::vector 相关的标准警告,并且它不是一个合适的容器。

#include <vector>    //for std::vector
#include <algorithm> //for std::reverse
#include <climits>   //for CHAR_BIT

template<typename T>
std::vector<bool> convert(T t) {
  std::vector<bool> ret;
  for(unsigned int i = 0; i < sizeof(T) * CHAR_BIT; ++i, t >>= 1)
    ret.push_back(t & 1);
  std::reverse(ret.begin(), ret.end());
  return ret;
}

还有一个[可能]也可以使用浮点值的版本。可能还有其他 POD 类型。我根本没有真正测试过这个。对于负值,它可能效果更好,也可能更差。我还没有考虑太多。

template<typename T>
std::vector<bool> convert(T t) {
  union {
    T obj;
    unsigned char bytes[sizeof(T)];
  } uT;
  uT.obj = t;

  std::vector<bool> ret;
  for(int i = sizeof(T)-1; i >= 0; --i) 
    for(unsigned int j = 0; j < CHAR_BIT; ++j, uT.bytes[i] >>= 1)
      ret.push_back(uT.bytes[i] & 1);
  std::reverse(ret.begin(), ret.end());
  return ret;
}

A modification of DCP's answer. The behavior is implementation defined for negative values of t. It provides all bits, even the leading zeros. Standard caveats related to the use of std::vector<bool> and it not being a proper container.

#include <vector>    //for std::vector
#include <algorithm> //for std::reverse
#include <climits>   //for CHAR_BIT

template<typename T>
std::vector<bool> convert(T t) {
  std::vector<bool> ret;
  for(unsigned int i = 0; i < sizeof(T) * CHAR_BIT; ++i, t >>= 1)
    ret.push_back(t & 1);
  std::reverse(ret.begin(), ret.end());
  return ret;
}

And a version that [might] work with floating point values as well. And possibly other POD types. I haven't really tested this at all. It might work better for negative values, or it might work worse. I haven't put much thought into it.

template<typename T>
std::vector<bool> convert(T t) {
  union {
    T obj;
    unsigned char bytes[sizeof(T)];
  } uT;
  uT.obj = t;

  std::vector<bool> ret;
  for(int i = sizeof(T)-1; i >= 0; --i) 
    for(unsigned int j = 0; j < CHAR_BIT; ++j, uT.bytes[i] >>= 1)
      ret.push_back(uT.bytes[i] & 1);
  std::reverse(ret.begin(), ret.end());
  return ret;
}
鸢与 2024-09-06 03:37:51

这是一个适用于负数的版本:

string get_bits(unsigned int x)
{
  string ret;
  for (unsigned int mask=0x80000000; mask; mask>>=1) {
    ret += (x & mask) ? "1" : "0";
  }
  return ret;
}

当然,该字符串可以用向量替换或对位值进行索引。

Here is a version that works with negative numbers:

string get_bits(unsigned int x)
{
  string ret;
  for (unsigned int mask=0x80000000; mask; mask>>=1) {
    ret += (x & mask) ? "1" : "0";
  }
  return ret;
}

The string can, of course, be replaced by a vector or indexed for bit values.

美人迟暮 2024-09-06 03:37:51

返回一个字符串而不是向量,但可以轻松更改。

template<typename T>
std::string get_bits(T value) {
    int size = sizeof(value) * CHAR_BIT;
    std::string ret;
    ret.reserve(size);
    for (int i = size-1; i >= 0; --i)
        ret += (value & (1 << i)) == 0 ? '0' : '1';
    return ret;
}

Returns a string instead of a vector, but can be easily changed.

template<typename T>
std::string get_bits(T value) {
    int size = sizeof(value) * CHAR_BIT;
    std::string ret;
    ret.reserve(size);
    for (int i = size-1; i >= 0; --i)
        ret += (value & (1 << i)) == 0 ? '0' : '1';
    return ret;
}
满栀 2024-09-06 03:37:51

世界上最糟糕的整数到位字节转换器:

#include <algorithm>
#include <functional>
#include <iterator>
#include <stdlib.h>

class zero_ascii_iterator: public std::iterator<std::input_iterator_tag, char>
{
public:
    zero_ascii_iterator &operator++()
    {
        return *this;
    }

    char operator *() const
    {
        return '0';
    }
};


char bits[33];

_itoa(value, bits, 2);
std::transform(
    bits, 
    bits + strlen(bits), 
    zero_ascii_iterator(), 
    bits, 
    std::minus<char>());

The world's worst integer to bit as bytes converter:

#include <algorithm>
#include <functional>
#include <iterator>
#include <stdlib.h>

class zero_ascii_iterator: public std::iterator<std::input_iterator_tag, char>
{
public:
    zero_ascii_iterator &operator++()
    {
        return *this;
    }

    char operator *() const
    {
        return '0';
    }
};


char bits[33];

_itoa(value, bits, 2);
std::transform(
    bits, 
    bits + strlen(bits), 
    zero_ascii_iterator(), 
    bits, 
    std::minus<char>());
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文