1. 程式人生 > >java中bit操作常用技巧

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. import java.io.*;  
  3. /** 
  4.  * 流操作工具類 
  5.  *  
  6.  * @author 崔素強 
  7.  */  
  8. public class StreamTool {  
  9.     /** 
  10.      * @方法功能 InputStream 轉為 byte 
  11.      * @param InputStream 
  12.      * @return 位元組陣列 
  13.      * @throws Exception 
  14.      */  
  15.     public static byte[] inputStream2Byte(InputStream inStream)  
  16.             throws Exception {  
  17.         // ByteArrayOutputStream outSteam = new ByteArrayOutputStream();  
  18.         // byte[] buffer = new byte[1024];  
  19.         // int len = -1;  
  20.         // while ((len = inStream.read(buffer)) != -1) {  
  21.         // outSteam.write(buffer, 0, len);  
  22.         // }  
  23.         // outSteam.close();  
  24.         // inStream.close();  
  25.         // return outSteam.toByteArray();  
  26.         int count = 0;  
  27.         while (count == 0) {  
  28.             count = inStream.available();  
  29.         }  
  30.         byte[] b = new byte[count];  
  31.         inStream.read(b);  
  32.         return b;  
  33.     }  
  34.     /** 
  35.      * @方法功能 byte 轉為 InputStream 
  36.      * @param 位元組陣列 
  37.      * @return InputStream 
  38.      * @throws Exception 
  39.      */  
  40.     public static InputStream byte2InputStream(byte[] b) throws Exception {  
  41.         InputStream is = new ByteArrayInputStream(b);  
  42.         return is;  
  43.     }  
  44.     /** 
  45.      * @功能 短整型與位元組的轉換 
  46.      * @param 短整型 
  47.      * @return 兩位的位元組陣列 
  48.      */  
  49.     public static byte[] shortToByte(short number) {  
  50.         int temp = number;  
  51.         byte[] b = new byte[2];  
  52.         for (int i = 0; i < b.length; i++) {  
  53.             b[i] = new Integer(temp & 0xff).byteValue();// 將最低位儲存在最低位  
  54.             temp = temp >> 8// 向右移8位  
  55.         }  
  56.         return b;  
  57.     }  
  58.     /** 
  59.      * @功能 位元組的轉換與短整型 
  60.      * @param 兩位的位元組陣列 
  61.      * @return 短整型 
  62.      */  
  63.     public static short byteToShort(byte[] b) {  
  64.         short s = 0;  
  65.         short s0 = (short) (b[0] & 0xff);// 最低位  
  66.         short s1 = (short) (b[1] & 0xff);  
  67.         s1 <<= 8;  
  68.         s = (short) (s0 | s1);  
  69.         return s;  
  70.     }  
  71.     /** 
  72.      * @方法功能 整型與位元組陣列的轉換 
  73.      * @param 整型 
  74.      * @return 四位的位元組陣列 
  75.      */  
  76.     public static byte[] intToByte(int i) {  
  77.         byte[] bt = new byte[4];  
  78.         bt[0] = (byte) (0xff & i);  
  79.         bt[1] = (byte) ((0xff00 & i) >> 8);  
  80.         bt[2] = (byte) ((0xff0000 & i) >> 16);  
  81.         bt[3] = (byte) ((0xff000000 & i) >> 24);  
  82.         return bt;  
  83.     }  
  84.     /** 
  85.      * @方法功能 位元組陣列和整型的轉換 
  86.      * @param 位元組陣列 
  87.      * @return 整型 
  88.      */  
  89.     public static int bytesToInt(byte[] bytes) {  
  90.         int num = bytes[0] & 0xFF;  
  91.         num |= ((bytes[1] << 8) & 0xFF00);  
  92.         num |= ((bytes[2] << 16) & 0xFF0000);  
  93.         num |= ((bytes[3] << 24) & 0xFF000000);  
  94.         return num;  
  95.     }  
  96.     /** 
  97.      * @方法功能 位元組陣列和長整型的轉換 
  98.      * @param 位元組陣列 
  99.      * @return 長整型 
  100.      */  
  101.     public static byte[] longToByte(long number) {  
  102.         long temp = number;  
  103.         byte[] b = new byte[8];  
  104.         for (int i = 0; i < b.length; i++) {  
  105.             b[i] = new Long(temp & 0xff).byteValue();  
  106.             // 將最低位儲存在最低位  
  107.             temp = temp >> 8;  
  108.             // 向右移8位  
  109.         }  
  110.         return b;  
  111.     }  
  112.     /** 
  113.      * @方法功能 位元組陣列和長整型的轉換 
  114.      * @param 位元組陣列 
  115.      * @return 長整型 
  116.      */  
  117.     public static long byteToLong(byte[] b) {  
  118.         

    相關推薦

    javabit操作常用技巧

    1. bit:位     一個二進位制資料0或1,是1bit; 2. byte:位元組     儲存空間的基本計量單位,如:MySQL中定義 VARCHAR(45)  即是指 45個位元組;     1 byte = 8 bit 3. 一個英文字元佔一個位元組;    

    JavaHashMap的常用操作

    前期準備:首先給hashMap裡面put一些鍵值對,程式碼如下: HashMap<Integer, Integer> hashMap = new HashMap<>(); hashMap.put(5, 2); hashMap.put(

    關於Java陣列的常用操作方法

    1. 宣告一個數組 1 String[] arr1 = new String[5]; 2 String[] arr2 = {"a","b","c", "d", "e"}; 3 String[] arr3= new String[]{"a","b","c","d",

    Java陣列操作 java.util.Arrays 類常用方法的使用

    任何一門程式語言,陣列都是最重要和常用的資料結構之一,但不同的語言對陣列的構造與處理是不盡相同的。 Java中提供了java.util.Arrays 類能方便地運算元組,並且它提供的所有方法都是靜態的。下面介紹一下Arrays類最常用的幾個方法。 1.  陣列排序 Arrays工具類提供了一個sor

    java 幾種常用數據結構

    初學 ble log app 使用 blog list 好的 sort Java中有幾種常用的數據結構,主要分為Collection和map兩個主要接口(接口只提供方法,並不提供實現),而程序中最終使用的數據結構是繼承自這些接口的數據結構類。 一、幾個常用類的區別 1.

    javaString類常用方法、屬性等

    col clas equal ack length ++ ava eal rgs package Head18; public class java09 { public static void main(String[] args) { St

    javaString的常用方法

    大寫 緩沖 let 常用方法 類型 http 全部 new 出現 轉自http://www.cnblogs.com/crazyac/articles/2012791.html java中String的常用方法1、length() 字符串的長度  例:char chars[]

    JAVA幾種常用的RPC框架介紹

    github 不同的 target int https love num 分布 有一個 RPC是遠程過程調用的簡稱,廣泛應用在大規模分布式應用中,作用是有助於系統的垂直拆分,使系統更易拓展。Java中的RPC框架比較多,各有特色,廣泛使用的有RMI、Hessian、Du

    Java 幾種常用的線程池

    需要 表示 ali adf data future rate 並發 ng- Java 中幾種常用的線程池 轉載 : https://www.cnblogs.com/sachen/p/7401959.html 原創 2016年04月14日 23:29:01 標簽: j

    bash shell 時間操作常用方法總結

    hour day 當前時間 簡單的 之前 nbsp seconds 獲取 相互   在日常的工作中,bash shell 的時間操作非常頻繁。比如shell腳本定時發送數據統計的時候,會查看當前是否為預定的發送時間。或者使用文件保存一些數據時,一般會生成時間字符串當做文

    Java幾個常用設計模式

    1.單例模式(有的書上說叫單態模式其實都一樣) 該模式主要目的是使記憶體中保持1個物件。看下面的例子: package org.sp.singleton; //方法一 public class Singleton { //將自身的例項物件設定為一個屬性,並加上S

    Java檔案操作

    java提供了一些實現類對檔案進行操作 File 對具體檔案(目錄)進行抽象表示。File類只用於表示檔案或者目錄的資訊(名稱、大小),不能用於檔案內容的訪問。 file類的方法比較多,以一個例項演示常用的幾個API。 import java.io.F

    JavaString類常用方法(轉)

    轉自:https://blog.csdn.net/kaishizhangcheng/article/details/52332543int indexOf(String str)該方法用於返回當給定字串在當前字串中的位置,若當前字串不包含給定字串則返回-1。過載的方法int

    java幾種常用的資料結構

    JAVA中有幾種常用的資料結構,主要分為Collection和map兩個主要介面(介面值提供方法,並不提供實現),而程式中最終使用的資料結構是繼承自這些介面的資料結構類。 Collcation: Map: 一、幾個常用類的區別  1.ArrayList: 元素單個,

    Java幾種常用的分頁

    ... List<StudentEnroll> students = studentlDao.getAllStudents(); int count = 0; if(studentEnrolls != null && studentEnrolls.s

    JavaMath類常用函式總結

    Java中比較常用的幾個數學公式的總結: //取整,返回小於目標函式的最大整數,如下將會返回-2 Math.floor(-1.8); //取整,返回發育目標數的最小整數 Math.ceil() //

    javaString類常用方法I(判斷 Java 檔名是否正確,判斷郵箱格式是否正確)

    內容摘自慕課網 具體程式碼: public class HelloWorld { public static void main(String[] args) { // Java檔名 String fileName

    JavaMath類常用函式總結】

    Java中比較常用的幾個數學公式的總結: //取整,返回小於目標函式的最大整數,如下將會返回-2 Math.floor(-1.8); //取整,返回發育目標數的最小整數 Math.ceil() //四捨五入取整 Math.round() //計算平

    Java幾個常用類總結

    一. System: 1.     首先是System類,因為從一開始從接觸java起,我們就無時無刻都在接觸它,經常用它來向螢幕,向控制檯列印輸出一些資訊,System.out.println(“hello world”);這個只是在控制檯輸出一條資訊“hello wor

    java 幾種常用資料結構

    Java中有幾種常用的資料結構,主要分為Collection和map兩個主要介面(介面只提供方法,並不提供實現),而程式中最終使用的資料結構是繼承自這些介面的資料結構類。 一、幾個常用類的區別  1.ArrayList: 元素單個,效率高,多用於查詢  2.Ve