bit operation on leading 8 bits

J

Julia Donawald

Hi,

I have the following problem, and till now sadly I couldnt find an
algorithm which does the job well. My problem is as follows:
Given for example the following bit-value ( saved as unsigned long ):
001101000001101010, so the unsigned long variable has the value:
53354. Furthermore I know the length of this bit-value ( here: 18 ).
Now I want to delete the leading 8 bits of such a bit value, so that
after the operation I get the value: 0001101010.
At the moment I solve this problem as follows, but I am not satisfied
with this solution:
unsigned long helper = (output << (16-output_len)) & 0xFFFF;
unsigned long output = helper >> (16-output_len);
The problem is now that if for example I only have an output-len > 16
then this algorithm wont work. I know I could solve this with a simple
if-else-block, but then I have again the problem what would happen if
the output len is <= 24 and so on. So I am searching for a more
general algorithm on that problem.

Thanks in advance for any help.

Bye,
Julia
 
J

Jan Engelhardt

Given for example the following bit-value ( saved as unsigned long ):
001101000001101010, so the unsigned long variable has the value:
53354. Furthermore I know the length of this bit-value ( here: 18 ).
Now I want to delete the leading 8 bits of such a bit value, so that
after the operation I get the value: 0001101010.

unsigned long res = in & 1023; // to keep the trailing 10 bits
 
J

Jan Engelhardt

Given for example the following bit-value ( saved as unsigned long ):
unsigned long res = in & 1023; // to keep the trailing 10 bits

So to round it up:

u32 res = in & ((1 << how_many_output_bits) - 1)
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Similar Threads


Members online

No members online now.

Forum statistics

Threads
473,774
Messages
2,569,598
Members
45,156
Latest member
KetoBurnSupplement
Top