c++ - Bitwise operator to get byte from 32 bits -


I'm interested in writing a function getMyByteChunkFunction which accepts two parameters - a 32 For example, this integer is given: -

  - For example, the bit integer and a byte offset (0, 1, 2, or 3), then removes bytes related to 32-bit integers. (3) (2) (1) (0) --- byte numbers int word = 10101010 00001001 11001010 00000101   

function call getMeByteChunkFunction (word, 2) Returns 00001001 .

However, I can use bitwise operators limited in it. Only me & gt; & Gt; , & lt; I am allowed to use a , and exactly one subtraction I know how to do this and using XOR, but I do not know how I use a subtraction here Any thoughts?

One idea is as follows: Suppose that you have a four-byte value:

  aaaaaaaa bbbbbbbb cccccccc dddddddd   

Suppose you want to get a byte BBBBBBBB out of this if you move right by two bytes If you do, you can

  ???????? Reciprocity Aaaaaaaa bbbbbbbb   

This value is equal to what you want, except the ???????? Reciprocity Aaaaaaa (because we are not sure if the change is signal-protected or not, because I do not know if your value is unsigned). No worries; We can get rid of these unknown values ​​and a bytes. To get rid of the top, let's make a change to another byte,

  ???????? Reciprocity Reciprocity Aaaaaaa   

Now, get a byte to get it

  ???????? Reciprocity Aaaaaaaa 00000000   

If you subtract this again, then you

  ???????? Reciprocity Aahaya bbbbbbbb - ???????? Reciprocity Aaaaaaa 00000000 --------------------------------------- 00000000 00000000 00000000 Bbbbbbbb  < / Ex> 

and voice ... you've got the value you want!

I will leave the actual code as a practice for the reader. do not worry; This is not particularly difficult: -)

Comments

Popular posts from this blog

mysql - BLOB/TEXT column 'value' used in key specification without a key length -

c# - Using Vici cool Storage with monodroid -

python - referencing a variable in another function? -