Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

XOR 128 bit bitsets

Tags:

c++

bitset

I am trying to XOR tow 128 bit bitsets.

#include<iostream>
#include<bitset>

int main()
{
  std::bitset<128> testing;
  testing = std::bitset<128>(0x544F4E20776E69546F656E772020656F) ^
  std::bitset<128>(0x5473206768204B20616D754674796E75);
  std::cout<<testing;
}

The output I get is enter image description here

The first 64 bits are 0 and the last 64 bits are XOR. I also get a compiler warning

warning: integer constant is too large for its type

Is there some way to XOR 128 bit bitsets or do I need to create an ugly hack?

like image 264
Pranav Kapoor Avatar asked Oct 20 '15 14:10

Pranav Kapoor


1 Answers

Your problem is not the XOR, but initializing the bitsets from a constant. As the warning says, there is a limit to the size that integer constants can have, and std::bitset constructor takes an unsigned long long which is usually 64 bits long.

You can initialize the bitsets from a binary string instead:

std::bitset<128>("100101010....")

Or combine it from two 64-bit bitsets:

std::bitset<128> value = (std::bitset<128>(0x1234567890123456) << 64) | 
                         std::bitset<128>(0x1234567890123456);
like image 161
interjay Avatar answered Oct 16 '22 15:10

interjay