Pranav Kapoor
Pranav Kapoor

Reputation: 1281

XOR 128 bit bitsets

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?

Upvotes: 6

Views: 1333

Answers (1)

interjay
interjay

Reputation: 110128

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);

Upvotes: 12

Related Questions