Java利用移位运算将int型分解成四个byte型的方法

2025-05-29 0 79
?

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38
package 移位运算;

public class 移位运算 {

public static void main(string[] args) {

//00000111 01011011 11001101 00010101

int n=123456789; //n为需要进行移位处理的32位int型初始值

byte[] a =chai(n);

for (int i = 0; i < a.length; i++) {

byte b=a[i];

system.out.print(b+" ");

}

//将数组重新合并成一个int型

system.out.println(hebing(a));

}

private static int hebing(byte[] arr) {

int n=0;

/*for (int i = 0; i < arr.length; i++) { //循环位或

n=n|arr[i]<<8*(arr.length-1)>>>8*i;

}

*

* n=n|arr[0]<<24;

* n=n|arr[1]<<24>>>8;

* n=n|arr[2]<<24>>>16;

* n=n|arr[3]<<24>>>24;

*/

for (int i = 0; i < arr.length; i++) { //循环位或

n+=(arr[i]&0x000000ff)<<8*(arr.length-1-i);

}

return n;

}

private static byte[] chai(int n) {

// 新建四个长度的byte数组

byte[] arr = new byte[4];

for (int i = 0; i < arr.length; i++) {

arr[i] = (byte) (n>>8*(arr.length-i-1));

}

return arr;

}

}

Java利用移位运算将int型分解成四个byte型的方法

总结

以上就是这篇文章的全部内容了,希望本文的内容对大家的学习或者工作具有一定的参考学习价值,谢谢大家对快网idc的支持。如果你想了解更多相关内容请查看下面相关链接

原文链接:https://blog.csdn.net/weixin_43810579/article/details/84845915

收藏 (0) 打赏

感谢您的支持,我会继续努力的!

打开微信/支付宝扫一扫,即可进行扫码打赏哦,分享从这里开始,精彩与您同在
点赞 (0)

声明:本站所有文章,如无特殊说明或标注,均为本站原创发布。任何个人或组织,在未征得本站同意时,禁止复制、盗用、采集、发布本站内容到任何网站、书籍等各类媒体平台。如若本站内容侵犯了原著者的合法权益,可联系我们进行处理。

快网idc优惠网 建站教程 Java利用移位运算将int型分解成四个byte型的方法 https://www.kuaiidc.com/110230.html

相关文章

发表评论
暂无评论