Preface :
Applications such as compiler code generation and compression algorithms create data that includes specific sequences of bits. For instance, the machine code for an addition instruction is ofter a specific sequence of 16 bits. Certain bits have fixed values, but others belong to bit fields that indicate the size of data, the source and the destination of the data for the operation.
Java allows manipulation of individual bits with the operators OR("|"), AND("&"), NOT("~"), and XOR("^"). The operators take operands of type int, long, short, byte and char. A short is a 16 bit integer in the range from -32768 to 32767, and a byte is an 8-bit integer value in the range from -128 to 127. Table below defines the operators :
Binary Numeric Promotion :
Before performing the bitwise operator |, &, or ^, Java performs binary numeric promotion on the operands. The type of the bitwise operator expression is the promoted type of the operands. The rules of promotion are as follows :
In this case of the unary operator ~, Java converts a byte, char or a short to int before applying the operator, and the resulting value is an int.
The BitArray Class :
Rather than requiring a programmer to use low-level bit operations, we develop the BitArray class as an alternative. A BitArray object treats a sequence of nbits as an array, with bit 0 the first bit on the left, bit 1 the second bit and n-1 the last bit on the right. For instance, if we use a bit array to represent the 5 bit stream 10111, bit 0 is 1, bit 1 is 0 and bit 2 through 4 are 1. To be effective, a bit array must supply operations that return the value of a particular bit in the bit stream and that set and clear individual bits. In addition, the class should provide methods that implement a set of bit-handling operations.
The first constructor creates a bit array all of whose bits are 0. The second constructor simplifies the creation of short bit vectors by using a Java integer array containing the values 1 and 0 to initialize the individual bits. Three methods, assignInt(), assingChar(), and assignByte(), allow a convenient conversion between an integer, a character or a byte and the corresponding bit array. The methods read() and write() implement I/O for a binary stream. We discuss binary streams in Section 23.2. The class has the toString() method, which returns a binary string representation of the bits in the array. The following is a skeletal listing of the class that includes private data members and constructions. The other methods are grouped into categories and displayed after the class listing :
Implementing the BitArray Class :
The BitArray class uses Java bit operations on individual bits in a byte to efficiently implement a BitArray object. An array of byte type stores the range of individual bits with numbers 0 to numberOfBits-1. The implementation views the array, called member, as a stream of bits. The element member[0] provides 8 bits, member[1] provides 8 more bits and so forth. There is a mapping from a bit in member to a bit number in the range 0 ... numberOfBits-1. The left-most bit of member[0] represents bits 0 and the right most bit of member[0] represents bit 7. We continue with the left-most bit of member[1] representing bit 8, and so forth. The following illustrates the storage scheme :
The private methods arrayIndex() and bitMask() implement the storage scheme. The method arrayIndex() determines the array element to which bit ibelongs. Simply divide i by 8. In this way, i=0 through i=7 belongs to member[0], i=8 through i-15 belongs to member[1] and so forth :
After locating the correct array index, apply the method bitMask() that returns a byte value containing a 1 in the bit position representing i. This value, called a mask, can be used to set or clear the bit :
- The BitArray Constructors
There are two constructors that create a BitArray object. One creates an empty bit array of a specified size; the second constructor initializes the bit array by using a Java integer array of 0 and 1 values. Build an empty bit array by determining the number of byte array elements needed to represent the range of bit numbers, allocate the array member to have the number of elements, and fill the array with 0 values.
- BitArray Operators
The class implements the bitwise operators or() ,and() ,xor() and not(), as well as the left/rigth shift operators. For instance, to implement the bitwise or() method, construct a BitArray object, tmp, of the same size as the number of bits in the current object and x. Assign its elements to be bitwise OR of the array element representing the current object and x. Return this new bit array as the value of the method. Note that the method throws the IllegalArgumentException if the current object and x do not have the same size :
- Bit Access and Modification Methods :
Methods such as bit() and clear() access individual bits by applying the private method arrayIndex() and bitMask(). The method bit() returns 1 if it finds a 1 in the bit corresponding to i, 0 if it does not.
The method clear() turns off the bit corresponding to i. The process uses the AND operator and a mask that contains all 1s except for the specific i bit. Create the mask by using the bitwise NOT operator ~.
This is a blog to track what I had learned and share knowledge with all who can take advantage of them
標籤
- [ 英文學習 ]
- [ 計算機概論 ]
- [ 深入雲計算 ]
- [ 雜七雜八 ]
- [ Algorithm in Java ]
- [ Data Structures with Java ]
- [ IR Class ]
- [ Java 文章收集 ]
- [ Java 代碼範本 ]
- [ Java 套件 ]
- [ JVM 應用 ]
- [ LFD Note ]
- [ MangoDB ]
- [ Math CC ]
- [ MongoDB ]
- [ MySQL 小學堂 ]
- [ Python 考題 ]
- [ Python 常見問題 ]
- [ Python 範例代碼 ]
- [心得扎記]
- [網路教學]
- [C 常見考題]
- [C 範例代碼]
- [C/C++ 範例代碼]
- [Intro Alg]
- [Java 代碼範本]
- [Java 套件]
- [Linux 小技巧]
- [Linux 小學堂]
- [Linux 命令]
- [ML In Action]
- [ML]
- [MLP]
- [Postgres]
- [Python 學習筆記]
- [Quick Python]
- [Software Engineering]
- [The python tutorial]
- 工具收集
- 設計模式
- 資料結構
- ActiveMQ In Action
- AI
- Algorithm
- Android
- Ansible
- AWS
- Big Data 研究
- C/C++
- C++
- CCDH
- CI/CD
- Coursera
- Database
- DB
- Design Pattern
- Device Driver Programming
- Docker
- Docker 工具
- Docker Practice
- Eclipse
- English Writing
- ExtJS 3.x
- FP
- Fraud Prevention
- FreeBSD
- GCC
- Git
- Git Pro
- GNU
- Golang
- Gradle
- Groovy
- Hadoop
- Hadoop. Hadoop Ecosystem
- Java
- Java Framework
- Java UI
- JavaIDE
- JavaScript
- Jenkins
- JFreeChart
- Kaggle
- Kali/Metasploit
- Keras
- KVM
- Learn Spark
- LeetCode
- Linux
- Lucene
- Math
- ML
- ML Udemy
- Mockito
- MPI
- Nachos
- Network
- NLP
- node js
- OO
- OpenCL
- OpenMP
- OSC
- OSGi
- Pandas
- Perl
- PostgreSQL
- Py DS
- Python
- Python 自製工具
- Python Std Library
- Python tools
- QEMU
- R
- Real Python
- RIA
- RTC
- Ruby
- Ruby Packages
- Scala
- ScalaIA
- SQLAlchemy
- TensorFlow
- Tools
- UML
- Unix
- Verilog
- Vmware
- Windows 技巧
- wxPython
訂閱:
張貼留言 (Atom)
[Git 常見問題] error: The following untracked working tree files would be overwritten by merge
Source From Here 方案1: // x -----删除忽略文件已经对 git 来说不识别的文件 // d -----删除未被添加到 git 的路径中的文件 // f -----强制运行 # git clean -d -fx 方案2: 今天在服务器上 gi...
-
前言 : 為什麼程序管理這麼重要呢?這是因為: * 首先,本章一開始就談到的,我們在操作系統時的各項工作其實都是經過某個 PID 來達成的 (包括你的 bash 環境), 因此,能不能進行某項工作,就與該程序的權限有關了。 * 再來,如果您的 Linux 系統是個...
-
屬性 : 系統相關 - 檔案與目錄 語法 : du [參數] [檔案] 參數 | 功能 -a | 顯示目錄中個別檔案的大小 -b | 以bytes為單位顯示 -c | 顯示個別檔案大小與總和 -D | 顯示符號鏈結的來源檔大小 -h | Hum...
-
來源自 這裡 說明 : split 是 Perl 中非常有用的函式之一,它可以將一個字串分割並將之置於陣列中。若無特別的指定,該函式亦使用 RE 與 $_ 變數 語法 : * split /PATTERN/,EXPR,LIMIT * split /...
沒有留言:
張貼留言