java中bit操作常用技巧

1. bit:位
    一个二进制数据0或1,是1bit;
2. byte:字节
    存储空间的基本计量单位,如:MySQL中定义 VARCHAR(45)  即是指 45个字节;
    1 byte = 8 bit
3. 一个英文字符占一个字节;
    1 字母 = 1 byte = 8 bit
4. 一个汉字占2个字节;
    1 汉字 = 2 byte = 16 bit
byte:一个字节(8位)(-128~127)(-2的7次方到2的7次方-1)
short:两个字节(16位)(-32768~32767)(-2的15次方到2的15次方-1)
int:四个字节(32位)(一个字长)(-2147483648~2147483647)(-2的31次方到2的31次方-1)
long:八个字节(64位)(-9223372036854774808~9223372036854774807)(-2的63次方到2的63次方-1)
float:四个字节(32位)(3.402823e+38 ~ 1.401298e-45)(e+38是乘以10的38次方,e-45是乘以10的负45次方)
double:八个字节(64位)(1.797693e+308~ 4.9000000e-324

 

Java中数据流的操作很多都是到byte的,但是在许多底层操作中是需要根据一个byte中的bit来做判断!

Java中要根据byte获得bit就要进行一些位操作,不过为了使用我直接给出解决方案,至于位操作的一些内容,回头再说!

 

Java代码   收藏代码
  1. package com.test;  
  2. import java.util.Arrays;  
  3. public class T {  
  4.     /** 
  5.      * 将byte转换为一个长度为8的byte数组,数组每个值代表bit 
  6.      */  
  7.     public static byte[] getBooleanArray(byte b) {  
  8.         byte[] array = new byte[8];  
  9.         for (int i = 7; i >= 0; i--) {  
  10.             array[i] = (byte)(b & 1);  
  11.             b = (byte) (b >> 1);  
  12.         }  
  13.         return array;  
  14.     }  
  15.     /** 
  16.      * 把byte转为字符串的bit 
  17.      */  
  18.     public static String byteToBit(byte b) {  
  19.         return ""  
  20.                 + (byte) ((b >> 7) & 0x1) + (byte) ((b >> 6) & 0x1)  
  21.                 + (byte) ((b >> 5) & 0x1) + (byte) ((b >> 4) & 0x1)  
  22.                 + (byte) ((b >> 3) & 0x1) + (byte) ((b >> 2) & 0x1)  
  23.                 + (byte) ((b >> 1) & 0x1) + (byte) ((b >> 0) & 0x1);  
  24.     }  
  25.     public static void main(String[] args) {  
  26.         byte b = 0x35// 0011 0101  
  27.         // 输出 [0, 0, 1, 1, 0, 1, 0, 1]  
  28.         System.out.println(Arrays.toString(getBooleanArray(b)));  
  29.         // 输出 00110101  
  30.         System.out.println(byteToBit(b));  
  31.         // JDK自带的方法,会忽略前面的 0  
  32.         System.out.println(Integer.toBinaryString(0x35));  
  33.     }  
  34. }  

 输出内容就是各个 bit 位的 0 和 1 值!

 

根据各个Bit的值,返回byte的代码:

Java代码   收藏代码
  1. /** 
  2.  * 二进制字符串转byte 
  3.  */  
  4. public static byte decodeBinaryString(String byteStr) {  
  5.     int re, len;  
  6.     if (null == byteStr) {  
  7.         return 0;  
  8.     }  
  9.     len = byteStr.length();  
  10.     if (len != 4 && len != 8) {  
  11.         return 0;  
  12.     }  
  13.     if (len == 8) {// 8 bit处理  
  14.         if (byteStr.charAt(0) == '0') {// 正数  
  15.             re = Integer.parseInt(byteStr, 2);  
  16.         } else {// 负数  
  17.             re = Integer.parseInt(byteStr, 2) - 256;  
  18.         }  
  19.     } else {// 4 bit处理  
  20.         re = Integer.parseInt(byteStr, 2);  
  21.     }  
  22.     return (byte) re;  
  23. }  


转自:http://cuisuqiang.iteye.com/blog/1695498


没别的,直接上代码!

 

Java代码   收藏代码
  1. package com.dst.util;  
  2.   
  3. import java.io.*;  
  4.   
  5. /** 
  6.  * 流操作工具类 
  7.  *  
  8.  * @author 崔素强 
  9.  */  
  10. public class StreamTool {  
  11.   
  12.     /** 
  13.      * @方法功能 InputStream 转为 byte 
  14.      * @param InputStream 
  15.      * @return 字节数组 
  16.      * @throws Exception 
  17.      */  
  18.     public static byte[] inputStream2Byte(InputStream inStream)  
  19.             throws Exception {  
  20.         // ByteArrayOutputStream outSteam = new ByteArrayOutputStream();  
  21.         // byte[] buffer = new byte[1024];  
  22.         // int len = -1;  
  23.         // while ((len = inStream.read(buffer)) != -1) {  
  24.         // outSteam.write(buffer, 0, len);  
  25.         // }  
  26.         // outSteam.close();  
  27.         // inStream.close();  
  28.         // return outSteam.toByteArray();  
  29.         int count = 0;  
  30.         while (count == 0) {  
  31.             count = inStream.available();  
  32.         }  
  33.         byte[] b = new byte[count];  
  34.         inStream.read(b);  
  35.         return b;  
  36.     }  
  37.   
  38.     /** 
  39.      * @方法功能 byte 转为 InputStream 
  40.      * @param 字节数组 
  41.      * @return InputStream 
  42.      * @throws Exception 
  43.      */  
  44.     public static InputStream byte2InputStream(byte[] b) throws Exception {  
  45.         InputStream is = new ByteArrayInputStream(b);  
  46.         return is;  
  47.     }  
  48.   
  49.     /** 
  50.      * @功能 短整型与字节的转换 
  51.      * @param 短整型 
  52.      * @return 两位的字节数组 
  53.      */  
  54.     public static byte[] shortToByte(short number) {  
  55.         int temp = number;  
  56.         byte[] b = new byte[2];  
  57.         for (int i = 0; i < b.length; i++) {  
  58.             b[i] = new Integer(temp & 0xff).byteValue();// 将最低位保存在最低位  
  59.             temp = temp >> 8// 向右移8位  
  60.         }  
  61.         return b;  
  62.     }  
  63.   
  64.     /** 
  65.      * @功能 字节的转换与短整型 
  66.      * @param 两位的字节数组 
  67.      * @return 短整型 
  68.      */  
  69.     public static short byteToShort(byte[] b) {  
  70.         short s = 0;  
  71.         short s0 = (short) (b[0] & 0xff);// 最低位  
  72.         short s1 = (short) (b[1] & 0xff);  
  73.         s1 <<= 8;  
  74.         s = (short) (s0 | s1);  
  75.         return s;  
  76.     }  
  77.   
  78.     /** 
  79.      * @方法功能 整型与字节数组的转换 
  80.      * @param 整型 
  81.      * @return 四位的字节数组 
  82.      */  
  83.     public static byte[] intToByte(int i) {  
  84.         byte[] bt = new byte[4];  
  85.         bt[0] = (byte) (0xff & i);  
  86.         bt[1] = (byte) ((0xff00 & i) >> 8);  
  87.         bt[2] = (byte) ((0xff0000 & i) >> 16);  
  88.         bt[3] = (byte) ((0xff000000 & i) >> 24);  
  89.         return bt;  
  90.     }  
  91.   
  92.     /** 
  93.      * @方法功能 字节数组和整型的转换 
  94.      * @param 字节数组 
  95.      * @return 整型 
  96.      */  
  97.     public static int bytesToInt(byte[] bytes) {  
  98.         int num = bytes[0] & 0xFF;  
  99.         num |= ((bytes[1] << 8) & 0xFF00);  
  100.         num |= ((bytes[2] << 16) & 0xFF0000);  
  101.         num |= ((bytes[3] << 24) & 0xFF000000);  
  102.         return num;  
  103.     }  
  104.   
  105.     /** 
  106.      * @方法功能 字节数组和长整型的转换 
  107.      * @param 字节数组 
  108.      * @return 长整型 
  109.      */  
  110.     public static byte[] longToByte(long number) {  
  111.         long temp = number;  
  112.         byte[] b = new byte[8];  
  113.         for (int i = 0; i < b.length; i++) {  
  114.             b[i] = new Long(temp & 0xff).byteValue();  
  115.             // 将最低位保存在最低位  
  116.             temp = temp >> 8;  
  117.             // 向右移8位  
  118.         }  
  119.         return b;  
  120.     }  
  121.   
  122.     /** 
  123.      * @方法功能 字节数组和长整型的转换 
  124.      * @param 字节数组 
  125.      * @return 长整型 
  126.      */  
  127.     public static long byteToLong(byte[] b) {  
  128.         long s = 0;  
  129.         long s0 = b[0] & 0xff;// 最低位  
  130.         long s1 = b[1] & 0xff;  
  131.         long s2 = b[2] & 0xff;  
  132.         long s3 = b[3] & 0xff;  
  133.         long s4 = b[4] & 0xff;// 最低位  
  134.         long s5 = b[5] & 0xff;  
  135.         long s6 = b[6] & 0xff;  
  136.         long s7 = b[7] & 0xff// s0不变  
  137.         s1 <<= 8;  
  138.         s2 <<= 16;  
  139.         s3 <<= 24;  
  140.         s4 <<= 8 * 4;  
  141.         s5 <<= 8 * 5;  
  142.         s6 <<= 8 * 6;  
  143.         s7 <<= 8 * 7;  
  144.         s = s0 | s1 | s2 | s3 | s4 | s5 | s6 | s7;  
  145.         return s;  
  146.     }  
  147. }  

 



转自:http://cuisuqiang.iteye.com/blog/1434442

  • 0
    点赞
  • 17
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值