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程式碼- package com.test;
- import java.util.Arrays;
- public class T {
- /**
- * 將byte轉換為一個長度為8的byte陣列,陣列每個值代表bit
- */
-
public static byte[] getBooleanArray(byte
- byte[] array = new byte[8];
- for (int i = 7; i >= 0; i--) {
- array[i] = (byte)(b & 1);
- b = (byte) (b >> 1);
- }
- return array;
- }
- /**
- * 把byte轉為字串的bit
- */
-
public static String byteToBit(byte b) {
- return ""
- + (byte) ((b >> 7) & 0x1) + (byte) ((b >> 6) & 0x1)
- + (byte) ((b >> 5) & 0x1) + (byte) ((b >> 4) & 0x1)
- + (byte) ((b >> 3) & 0x1) + (byte) ((b >> 2) & 0x1)
- + (byte) ((b >> 1) & 0x1) + (byte) ((b >> 0) & 0x1);
- }
- public static void main(String[] args) {
- byte b = 0x35; // 0011 0101
- // 輸出 [0, 0, 1, 1, 0, 1, 0, 1]
- System.out.println(Arrays.toString(getBooleanArray(b)));
- // 輸出 00110101
- System.out.println(byteToBit(b));
- // JDK自帶的方法,會忽略前面的 0
- System.out.println(Integer.toBinaryString(0x35));
- }
- }
輸出內容就是各個 bit 位的 0 和 1 值!
根據各個Bit的值,返回byte的程式碼:
Java程式碼- /**
- * 二進位制字串轉byte
- */
- public static byte decodeBinaryString(String byteStr) {
- int re, len;
- if (null == byteStr) {
- return 0;
- }
- len = byteStr.length();
- if (len != 4 && len != 8) {
- return 0;
- }
- if (len == 8) {// 8 bit處理
- if (byteStr.charAt(0) == '0') {// 正數
- re = Integer.parseInt(byteStr, 2);
- } else {// 負數
- re = Integer.parseInt(byteStr, 2) - 256;
- }
- } else {// 4 bit處理
- re = Integer.parseInt(byteStr, 2);
- }
- return (byte) re;
- }
轉自:http://cuisuqiang.iteye.com/blog/1695498
沒別的,直接上程式碼!
Java程式碼- package com.dst.util;
- import java.io.*;
- /**
- * 流操作工具類
- *
- * @author 崔素強
- */
- public class StreamTool {
- /**
- * @方法功能 InputStream 轉為 byte
- * @param InputStream
- * @return 位元組陣列
- * @throws Exception
- */
- public static byte[] inputStream2Byte(InputStream inStream)
- throws Exception {
- // ByteArrayOutputStream outSteam = new ByteArrayOutputStream();
- // byte[] buffer = new byte[1024];
- // int len = -1;
- // while ((len = inStream.read(buffer)) != -1) {
- // outSteam.write(buffer, 0, len);
- // }
- // outSteam.close();
- // inStream.close();
- // return outSteam.toByteArray();
- int count = 0;
- while (count == 0) {
- count = inStream.available();
- }
- byte[] b = new byte[count];
- inStream.read(b);
- return b;
- }
- /**
- * @方法功能 byte 轉為 InputStream
- * @param 位元組陣列
- * @return InputStream
- * @throws Exception
- */
- public static InputStream byte2InputStream(byte[] b) throws Exception {
- InputStream is = new ByteArrayInputStream(b);
- return is;
- }
- /**
- * @功能 短整型與位元組的轉換
- * @param 短整型
- * @return 兩位的位元組陣列
- */
- public static byte[] shortToByte(short number) {
- int temp = number;
- byte[] b = new byte[2];
- for (int i = 0; i < b.length; i++) {
- b[i] = new Integer(temp & 0xff).byteValue();// 將最低位儲存在最低位
- temp = temp >> 8; // 向右移8位
- }
- return b;
- }
- /**
- * @功能 位元組的轉換與短整型
- * @param 兩位的位元組陣列
- * @return 短整型
- */
- public static short byteToShort(byte[] b) {
- short s = 0;
- short s0 = (short) (b[0] & 0xff);// 最低位
- short s1 = (short) (b[1] & 0xff);
- s1 <<= 8;
- s = (short) (s0 | s1);
- return s;
- }
- /**
- * @方法功能 整型與位元組陣列的轉換
- * @param 整型
- * @return 四位的位元組陣列
- */
- public static byte[] intToByte(int i) {
- byte[] bt = new byte[4];
- bt[0] = (byte) (0xff & i);
- bt[1] = (byte) ((0xff00 & i) >> 8);
- bt[2] = (byte) ((0xff0000 & i) >> 16);
- bt[3] = (byte) ((0xff000000 & i) >> 24);
- return bt;
- }
- /**
- * @方法功能 位元組陣列和整型的轉換
- * @param 位元組陣列
- * @return 整型
- */
- public static int bytesToInt(byte[] bytes) {
- int num = bytes[0] & 0xFF;
- num |= ((bytes[1] << 8) & 0xFF00);
- num |= ((bytes[2] << 16) & 0xFF0000);
- num |= ((bytes[3] << 24) & 0xFF000000);
- return num;
- }
- /**
- * @方法功能 位元組陣列和長整型的轉換
- * @param 位元組陣列
- * @return 長整型
- */
- public static byte[] longToByte(long number) {
- long temp = number;
- byte[] b = new byte[8];
- for (int i = 0; i < b.length; i++) {
- b[i] = new Long(temp & 0xff).byteValue();
- // 將最低位儲存在最低位
- temp = temp >> 8;
- // 向右移8位
- }
- return b;
- }
- /**
- * @方法功能 位元組陣列和長整型的轉換
- * @param 位元組陣列
- * @return 長整型
- */
- public static long byteToLong(byte[] b) {
-
相關推薦
java中bit操作常用技巧
1. bit:位 一個二進位制資料0或1,是1bit; 2. byte:位元組 儲存空間的基本計量單位,如:MySQL中定義 VARCHAR(45) 即是指 45個位元組; 1 byte = 8 bit 3. 一個英文字元佔一個位元組;
Java中HashMap的常用操作
前期準備:首先給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.
java中String類常用方法、屬性等
col clas equal ack length ++ ava eal rgs package Head18; public class java09 { public static void main(String[] args) { St
java中String的常用方法
大寫 緩沖 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
Java中String類常用方法(轉)
轉自: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
Java中Math類常用函式總結
Java中比較常用的幾個數學公式的總結: //取整,返回小於目標函式的最大整數,如下將會返回-2 Math.floor(-1.8); //取整,返回發育目標數的最小整數 Math.ceil() //
java中String類常用方法I(判斷 Java 檔名是否正確,判斷郵箱格式是否正確)
內容摘自慕課網 具體程式碼: public class HelloWorld { public static void main(String[] args) { // Java檔名 String fileName
【Java中Math類常用函式總結】
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