Java利用移位运算将int型分解成四个byte型的方法
作者:哲别君 时间:2023-11-09 08:25:00
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;
}
}
来源:https://blog.csdn.net/weixin_43810579/article/details/84845915
标签:java,int,byte,移位运算,分解
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
Android开发使用URLConnection进行网络编程详解
2022-01-07 22:49:28
![](https://img.aspxhome.com/file/2023/4/138624_0s.png)
Android实现CoverFlow效果控件的实例代码
2023-06-23 13:12:43
![](https://img.aspxhome.com/file/2023/5/83545_0s.png)
Mybatis中 mapper-locations和@MapperScan的作用
2023-07-13 08:08:34
![](https://img.aspxhome.com/file/2023/0/57700_0s.png)
java数据结构与算法数组模拟队列示例详解
2021-07-23 16:50:24
![](https://img.aspxhome.com/file/2023/0/80980_0s.png)
C#集合查询Linq在项目中使用详解
2022-09-16 13:50:09
Android自定义控件之日期选择控件使用详解
2021-07-11 09:39:52
![](https://img.aspxhome.com/file/2023/3/138603_0s.jpg)
C#调用Windows的API实现窗体动画
2022-09-09 13:05:22
![](https://img.aspxhome.com/file/2023/4/113764_0s.png)
Java Collections.shuffle()方法案例详解
2023-11-24 15:53:16
Unity实现游戏伤害数字显示HUD的方法
2023-05-18 06:47:01
![](https://img.aspxhome.com/file/2023/6/113816_0s.gif)
基于Android AIDL进程间通信接口使用介绍
2021-12-28 05:15:22
![](https://img.aspxhome.com/file/2023/7/113297_0s.jpg)
Spring定时任务无故停止又不报错的解决
2021-06-06 08:41:27
SpringIOC框架的简单实现步骤
2021-10-28 21:44:35
理解Java当中的回调机制(翻译)
2023-03-15 04:21:00
![](https://img.aspxhome.com/file/2023/0/89610_0s.png)
java基础之包装类的介绍及使用
2023-01-10 17:45:29
![](https://img.aspxhome.com/file/2023/5/61255_0s.png)
Android实现带动画效果的可点击展开TextView
2023-08-06 05:42:08
![](https://img.aspxhome.com/file/2023/9/85989_0s.png)
Android AlertDialog六种创建方式案例详解
2022-08-30 01:55:47
![](https://img.aspxhome.com/file/2023/1/137691_0s.jpg)
SpringBoot整合Mybatis与thymleft实现增删改查功能详解
2023-01-06 00:22:59
![](https://img.aspxhome.com/file/2023/2/118822_0s.png)
Android集成zxing扫码框架功能
2021-09-27 02:37:17
![](https://img.aspxhome.com/file/2023/8/120098_0s.png)
非常适合新手学生的Java线程池超详细分析
2023-03-03 11:59:54
idea2020.1无法自动加载maven依赖的jar包问题及解决方法
2021-06-13 17:40:36
![](https://img.aspxhome.com/file/2023/6/88506_0s.jpg)