Java shl和shr填充问题
有没有一种方法可以在不丢失字节的情况下向左或向右移动,以便填充的字节是正在占用的字节?
例如:10010 shr 2 => 10100 或:11001 shl 4 => 11100
信息丢失似乎很不方便,因为无论如何你都不应该将它用于数学。
我只想以不同的字节顺序通过网络发送包,所以向后移动对我来说很重要
Is there a way to shift left or right without the byte loss, so that the bytes filled are the ones that are beeing taken?
e.g.:10010 shr 2 => 10100
or: 11001 shl 4 => 11100
the loss of information seems quite inconvenient, since you're not supposed to use it for math anyway..
i just want to send packages over the network in different byte order, so shifting back is important to me
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
你想要做的是Java支持的按位循环。
这将打印出:
被移走的 2 位已循环到开头。但是中间有很多0填充,因为Java中的整数占用32位。
如果您想自己实现此行为,则在内部将其实现为:
并且:
What you're trying to do is bitwise rotation which is supported in Java.
This will print out:
The 2 bits which were shifted off have been rotated round to the start. However there is a lot of 0 padding in the middle because an integer in Java takes up 32 bits.
If you wanted to implement this behaviour yourself, internally it is implemented as:
And: