I’ve got a long variable and I need to reverse its byte order. For example: B1, B2, ... , B8 I should return a long that consists of B8, B7, ..., B1. How can I do it by using bitwise operations?
I’ve got a long variable and I need to reverse its byte order. For
Share
you can use Long.reverseBytes(long)
Or for more methods which include bitwise operations, you can refer to this stack overflow question
Heres another method you may like, I’d still recommend the above but it’s better than bitwise where you can easily make mistakes.
Bytebuffer
I am trying to steer you away from bitwise solutions because they are cumbersome and very easy to mess up if you do not know what you are doing… But bitwise would look like this: