假设我们在Java中有两个BitSet对象,其值为
//<MSB....LSB>
B1:<11000101>
B2:<10111101>
Run Code Online (Sandbox Code Playgroud)
我们如何比较B1和B2才能知道B1代表的值大于B2代表的值.
是否为BitSet重载了逻辑运算符(>,<,==)?或者我是否必须编写自己的实现?
更新:刚发现"运算符>未定义参数类型java.util.BitSet,java.util.BitSet".有没有内置方法可以这样做?
在我的应用程序中,我试图显示双变量的位表示。它适用于较小的双变量。不适用于 10^30 级别。
代码:
#include <iostream>
#include <bitset>
#include <limits>
#include <string.h>
using namespace std;
void Display(double doubleValue)
{
bitset<sizeof(double) * 8> b(doubleValue);
cout << "Value : " << doubleValue << endl;
cout << "BitSet : " << b.to_string() << endl;
}
int main()
{
Display(1000000000.0);
Display(2000000000.0);
Display(3000000000.0);
Display(1000000000000000000000000000000.0);
Display(2000000000000000000000000000000.0);
Display(3000000000000000000000000000000.0);
return 0;
}
Run Code Online (Sandbox Code Playgroud)
输出:
/home/sujith% ./a.out
Value : 1e+09
BitSet : 0000000000000000000000000000000000111011100110101100101000000000
Value : 2e+09
BitSet : 0000000000000000000000000000000001110111001101011001010000000000
Value : 3e+09
BitSet : 0000000000000000000000000000000010110010110100000101111000000000
Value : 1e+30
BitSet : …Run Code Online (Sandbox Code Playgroud) 我刚刚发现java中有BitSet。已经有数组和类似的数据结构。BitSet可以用在什么地方?
我很困惑BitSet。BitSet数据结构是否存储 1 和 0?
val b = BitSet(0, 2, 3)
Run Code Online (Sandbox Code Playgroud)
意味着为位位置 0、2 和 3 存储 1?
如果是这样,最大值是多少?不。位,32 还是 64?
我正在学习Bloom filter和BitMap(也称为Bit Array)并遇到一个问题,有人能给我一些关于何时使用布隆过滤器以及何时使用 BitMap 的说明吗?
在我的理解中我认为当我们需要找到最大的数字或者想要对庞大的数据进行排序时,BitMap 更适合(对于纯数字)。
如果我们想检查一些IP地址是否包含在数十亿条现有记录中,那么布隆过滤器更适合(用于字符串或其他非纯数字)。
但是,我想有人给我更详细的说明或建议,我在谷歌上搜索过,没有找到一些有用的信息。提前致谢!
另外我不知道我是否应该将这个问题放在stackoverflow或其他站点上,如果它不是正确的站点,希望有人指出,谢谢!
我目前正在尝试声明一个包含 17 个 std::bitset 的数组,每个 32 位长。我是这样做的:
std::bitset<32> mTestInstruction[17]
{
std::string("01000000001000000000000000000001"),
std::string("01000000011000000000000001100011"),
std::string("01000000101000000000000000000001"),
std::string("10100000000000000000000000001010"),
std::string("00000000100000010000000010000010"),
std::string("00000000110001010010000000000001"),
std::string("01001000111001010000000000000000"),
std::string("01000100001000110000000000000011"),
std::string("01000000001000010000000000000001"),
std::string("10000000000000000000000000000011"),
std::string("00000000010000000000000000000001"),
std::string("00000000111000000000000000000001"),
std::string("00000000111001110000100000000001"),
std::string("01000000010000100000000000000001"),
std::string("01000100001000100000000000000010"),
std::string("10000000000000000000000000001100"),
std::string("11100000000000000000000000001000"),
};
Run Code Online (Sandbox Code Playgroud)
我收到以下错误:
error: could not convert 'std::__cxx11::basic_string<char>(((const char*)"01000000001000000000000000000001"), std::allocator<char>())' from 'std::__cxx11::string {aka std::__cxx11::basic_string<char>}' to 'std::bitset<32u>'
对于每个位串。
我不明白为什么会发生这种情况,因为根据 cpp 参考, std::string 是构造位集的有效方法。谁能指出如何解决这个问题?
我有一个浮点变量,每一步递增 0.1。我想将其转换为 16 位固定值,其中有 5 位小数部分。为了做到这一点,我有下面的代码片段:
#include <iostream>
#include <bitset>
#include <string>
using namespace std;
int main() {
bitset<16> mybits;
string mystring;
float x = 1051.0;
for (int i = 0; i < 20; i++)
{
mybits = bitset<16>(x*32);
mystring = mybits.to_string<char, string::traits_type, string::allocator_type>();
cout << x << "\t" << "mystring: " << mystring << '\n';
x += 0.1;
}
return 0;
}
Run Code Online (Sandbox Code Playgroud)
然而,结果是这样的:
1051 mystring: 1000001101100000
1051.1 mystring: 1000001101100011
1051.2 mystring: 1000001101100110
1051.3 mystring: 1000001101101001
1051.4 mystring: 1000001101101100
1051.5 …Run Code Online (Sandbox Code Playgroud) Java 的类有一个将单个位设置为 1 (=true) 的BitSet方法。Set该方法源码如下:
public void set(int bitIndex) {
if (bitIndex < 0)
throw new IndexOutOfBoundsException("bitIndex < 0: " + bitIndex);
int wordIndex = wordIndex(bitIndex);
expandTo(wordIndex);
words[wordIndex] |= (1L << bitIndex); // Restores invariants
checkInvariants();
}
Run Code Online (Sandbox Code Playgroud)
除了检查之外,该方法的核心代码是:words[wordIndex] |= (1L << bitIndex)。我可以在作业中清楚地看到,左侧部分是保存相关位的特定单词。但是,我不明白右侧部分(位索引的左移)如何导致请求的(并且只有它)位设置为 1。您能解释一下吗?
我试图将 45 位二进制数转换为十六进制数,但在编译时出现溢出错误,但在在线 C++ 编译器上应用代码时,它可以工作。我的平台是X64。请提供任何帮助。
int main()
{
stringstream ss;
string binary_str("111000000100010010100000110101001000100011000");
bitset<45> n(binary_str);
string f;
ss << hex << n.to_ulong() << endl; // error happens here
f = ss.str();
cout << f;
return 0;
}
Run Code Online (Sandbox Code Playgroud)
当在在线 C++ 编译器上编译上面的代码时,我得到了正确的结果,即 OX1c08941a9118。
我正在使用Java(JDK 1.8)开发并操作BitSets.我遇到了一个奇怪的问题.
我正在实例化一个大小为160的BitSet,如:
BitSet example = new BitSet(160);
Run Code Online (Sandbox Code Playgroud)
我想使用size()方法检查大小,该方法给出bitset中的位数.在文档中,据说具有int N作为参数的构造函数正在创建N位的位集.
但是,当我做检查大小之后用
example.size()
Run Code Online (Sandbox Code Playgroud)
我获得了价值
192
Run Code Online (Sandbox Code Playgroud)
我不明白为什么,有没有人遇到过这种问题?链接到文档:http://docs.oracle.com/javase/7/docs/api/java/util/BitSet.html