Friday 15 May 2015

java - Having difficulty understanding an example of when bit shifting should/needs to be used -


I should be using a bit shift operator when I was looking at that time. I understand that we do not have to use it to multiply in two more ways, because JIT compilation will take care of that I have passed, and I am confused by the part of the accepted answer: for example, my There are two bytes that are two-byte high order and low order byte (16-BT) unsigned value. Say you have to build that value in Java, that is: int high = ...; Int cum = ...; Int doubletew = = (high

I know that I am missing something, because it seems to me that it is only increasing to 2 ^ 8 and adding to reduce it (I have never seen | was used earlier in this context, but when I plugged in dummy values ​​and triggered my code, it seemed that it was just adding two together Was) really running here?

Edit: For reference, I had high = 10 and low = 3 .

For example, the number 54321: 1101 0100 0011 0001 high -Order allows writing 16-bit presentations from 8 bits low-order 8 bits.

High-order will be 8 bits: 1101 0100 will be 8-bit orders: 0011 0001

  1101 0100 & lt; & Lt;   

will generate

  1101 0100 0000 0000   

We can then bitwise or ( | ) Against low command bits

  1101 0100 0000 0000 0011 0001 ------------------- 1101 0100 0011 0001   

Now we have complete bipartisan representation at 54321 (assuming I am definitely using unsigned inputs)


Edit: To use your example : High = 10 and less = 3

High , written in 8 bit, this 0000 1010 will

decrease , Written in the same fashion, it will be 0000 0011

If we move high in left 8 bits: <00> 0000 0000 0000

If against or less:

  0000 0000 0000 0000 00 00 0011 - ----------------- 0000 1010 0000 0011   

If we consider this pattern to be a decimal integer, it would mean 2563

Perhaps the deceptive part is that 10 and < Code> 3 Do not really hold in. In this context everyone means that it is the composition of both of them Land that has value.

Maybe you are reading a file byte by byte, but the sequence of 16-bit integers in one part of the file is that you have to take every pair of bytes and get them to get a 16-bit integer In this fashion you have to combine.

Now imagine, on one platform where the biggest integer is 64 bits, you wanted to store an integer that is so large that it has 128 bits. Well, you can use this same trick to tease and store math, which is actually a bigger integer in two different values. Well, maybe more complicated than this example, but hopefully it makes a difference to the house, we need a betwoman operator like this.

No comments:

Post a Comment