Hey all
I am pretty new to messing around with the bits of a system, and I am trying to learn to use bitsets as a way to solve a few problems I am having. My question is this:
is there a way to use memcpy to copy the bits of say an integer into a certain place in a bitset? Say I want to make a key from my three ints such that the first 8 bits of the bitset are the 8 bits of integer one, the second 8 bits of bitset and the 8 bits of integer two, etc.
I want to do something like this:
Code:
#include <bitset>
#include <iostream>
#include <limits>
#include <cstring>
using namespace std;
int main( )
{
bitset<24> key;
int num1 = 5;
int num2 = 7;
int num3 = 2;
memcpy(&key, &num3, 8);
memcpy(&key+8, &num2, 8);
memcpy(&key+16, &num1, 8);
cout << key << endl;
}
Since bits are reversed, I would like the output from that to be:
00000101 00000111 00000010, but it most definitely isn't. I am completely lost in the fact that numbers are reversed in their binary representation on little endian systems (apparently?) and would really appreciate if someone could explain to me a little better than the tutorials I have found so far.
Also, declaring key as
just segfaults me when i try to use it like that, so is there anything diffferent about pointers to bitsets than regular pointers? I usualyl use c, so c++ is a little bit new to me.