views:

680

answers:

8
+1  Q: 

Bit Operators

If I have two things which are hex, can I someone how append their binary together to get a value?

In C++, say I have

unsigned char t = 0xc2;  // 11000010
unsigned char q = 0xa3;  // 10100011

What I want is somehow, 1100001010100011, is this possible using bit-wise operators?

I want to extract the binary form of t and q and append them...

A: 

There is no append in binary/hex because you are dealing with Numbers (can you append 1 and 2 and not confuse the resulting 12 with the "real" 12?)

You could delimit them with some special symbol, but you can't just "concatenate" them.

Tigraine
A: 

Your example doesn't really work too well because the width (usually 8-bits) of the input values aren't defined. For example, why isn't your example: 0000000100000010, which would be truly appending 1 (00000001) and 2 (00000010) bit wise.

If each value does have a fixed width then it can be answered with bit shifting and ORing values

EDIT: if your "width" is defined the full width with all leading zero's removed, then it is possible to do with shifting and ORing, but more complicated.

Evan Teran
A: 

Appending as an operation doesn't really make sense for numbers, regardless of what base they're in. Using . as the concatenation operator: in your example, 0x1 . 0x2 becomes 0x12 if you concat the hex, and 0b101 if you concat the binary. But 0x12 and 0b101 aren't the same value (in base 10, they're 18 and 5 respectively). In general, A O B (where A and B are numbers and O is an operator) should result in the same value no matter what base you're operating in.

dirtside
he's asking for it in binary =P. that is a well defined operation. this also can actually be useful when doing stuff like huffman encoding.
Claudiu
A: 

First, some clarification - two numbers aren't "hex" or "binary" or "decimal" - they're just numbers. You can choose to represent them however you like. I'll assume they're just 4-byte integers for now.


The cop-out is to manipulate their representations, which is what you're doing anyway. So you can do something like this:

def binaryAppend(num1, num2):
    return int(dec2bin(str(num1)) + dec2bin(str(num2)))

This takes two integers, converts them to a string representation in decimal, then converts that to binary, then concatenates them together and returns that as an integer.

This is slow, however.

You can do this instead:

int binaryLength(char num) {
    int l = 0;
    while(num) {
        num >>= 1;
        l += 1;
    }
    return l;
}

This returns the number of binary digits in the number (shifts to the right until the number is 0). Then your concat function will look like this (You stated chars as input in your code, so I'll use chars here):

uint32 concatBin(char num1, char num2) {
    return num1 | (num2 << binaryLength(num1));
}

Example usage:

> concatBin(1, 2)
5

1 is 1 in binary, 2 is 10, the concat is 101, which is indeed 5 in decimal.

Claudiu
A: 

Sorry for the confusion,

In C++, say I have

unsigned char t = 0xc2;  // 11000010
unsigned char q = 0xa3;  // 10100011

What I want is somehow, 1100001010100011, is this possible using bit-wise operators?

I want to extract the binary form of t and q and append them...

BobS
Yes - shift t left 8 times (t << 8) and then OR it (| operator) with q.
dirtside
+18  A: 

Yes it's possible.

Just use the left-bitshift operator, shifting to the left by 8, using at least a 16-bit integer. Then binary OR the 2nd value to the integer.

unsigned char t = 0xc2; // 11000010 
unsigned char q = 0xa3; // 10100011
unsigned short s = (((unsigned short)t)<<8) | q; //// 11000010 10100011

Alternatively putting both values in a union containing 2 chars (careful of big endian or small) would have the same bit level result. Another option is a char[2].

Brian R. Bondy
In your last sentence, assuming you're talking about unioning a char[2] with a short, it depends on endianness.
Jim Buck
plus, if you use << 8 on an unsigned char, you end up with zero (because an unsigned char is 8 bits, you shift all the bits out of range). Cast t to a short before you do the shift.
OJ
fixed, good catch
Brian R. Bondy
A: 

Concatenating two chars:

unsigned char t = 0xc2;  // 11000010
unsigned char q = 0xa3;  // 10100011

int result = t;  // Put into object that can hold the fully concatenated data;
result <<= 8;     // Shift it left
result |= q;     // Or the bottom bits into place;
Martin York
That should be result <<= 8; , not result << 8;
therefromhere
A: 
baash05