-1

I understand that bitwise-operations on numbers represented by native data-types in C, is a piece of cake.

However, is there a way to do the same for large numbers represented by character arrays?

I tried googling, but surprisingly couldn't find an answer. So, will be heartfully grateful for any pointers.

Thanks and Regards, Ajay

Ajay Garg
  • 79
  • 1
  • 1
  • 4

1 Answers1

0

No, it's not possible.

In order to compute a boolean operation on such a vector, it would be needed to break it into chunks of an integer number of bits.

For example, if you have the numeral 378, each digit represent log2(8) = 3 bits.
You can take each digit, perform a 3 bit boolean operation and compute the final result by shifting each digit result:

378 AND 168 = (3 AND 1)·23 + (7 AND 6) = 1·23 + 6 = 168 = 1410.

Where the bold 3 is the number of bits of each digit as stated above.

If you apply the same reasoning with a base ten numeral, like 3710, you see that each digits represents

log2(10) ≈ 3.32192...

Since that is not and integral number you cannot perform a shift by 23.32192..., however the problem doesn't lie in the fact that log2(10) is not an integer;
If you had that each digit represented 1.5 bits, you could take 2 digits at a time to form a 3 bit group. Basically you would work by splitting the vector every two characters, converting them to binary, doing the boolean operation and shifting back the partial results into the final one.
This is exactly how base64 works that takes four 6 bit digits to make three 8 bit digits.

The problem with log2(10) is that it is irrational, so you can never find two integers n and k such that

log2(10) · n = k

because the equation is equivalent to k/n = log2(10) and the left hand side is rational but the right hand one is not.

So you cannot break the array into digits (or groups of digits), compute the partial results and combine the results back with shifts.

Margaret Bloom
  • 41,768
  • 5
  • 78
  • 124