How to get the individual bit values of byte and common bit operations

Source: Internet
Author: User

In the project, the data from the signal collecting board gets the information of the dashboard indicator light on the vehicle, and the received data is converted to byte, and the individual bit values of each byte are separated so as to know the specific information that each bit's value represents. Here is a note on how to get the individual bit values of byte, and some common bit operations are also recorded.

1. Isolate the value of each bit of a byte

An English character takes up one byte (1 letters =1 byte=8 bit), and one kanji is two bytes (1 kanji =2 byte=16 bit).

Where bit: bits, a binary data 0 or 1, is 1bit.

Byte: Bytes, the basic unit of measure for storage space, 1 byte=8 bit.

Byte:1 bytes (8-bit) ( -128~127) ( -2^7~2^7-1)

Short:2 bytes (16-bit) ( -32768~32767) ( -2^15~2^25-1)

Int:4 bytes (32-bit) ( -2147483648~2147483647) ( -2^31~2^31-1)

Long:8 bytes (84-bit) (9223372036854774808~9223372036854774807) ( -2^63~2^63-1)

Float:4 bytes (32 bits) (3.402823E+38~1.401298E-45) (e+ is multiplied by 10 for 38 times, e-45 is multiplied by 10 minus 45 times)

Double:8 bytes (64 bits) (1.797693e~4.9000000e-324)

(1) Byte-->bit

 Public classT {/*** Converts a byte to a byte array of length 8, where each value represents a bit*/       Public Static byte[] Getbooleanarray (byteb) {byte[] Array =New byte[8];  for(inti = 7; I >= 0; i--) {Array[i]= (byte) (B & 1); b= (byte) (b >> 1); }          returnArray; }      /*** bit to convert byte to string*/       Public StaticString Bytetobit (byteb) {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 voidMain (string[] args) {byteb = 0x35;//0011 0101//output [0, 0, 1, 1, 0, 1, 0, 1]System.out.println (arrays.tostring (Getbooleanarray (b))); //Output 00110101System.out.println (Bytetobit (b)); //The jdk comes with a method that ignores the previous 0System.out.println (Integer.tobinarystring (0x35)); }  }

(2) Bit-->byte

/*** binary string to byte*/   Public Static bytedecodebinarystring (String bytestr) {intRe, Len; if(NULL==bytestr) {          return0; } Len=bytestr.length (); if(Len! = 4 && len! = 8) {          return0; }      if(len = = 8) {//8 bit processing        if(Bytestr.charat (0) = = ' 0 ') {//PositiveRe = Integer.parseint (Bytestr, 2); } Else{//Negative numberRe = Integer.parseint (Bytestr, 2)-256; }      } Else{//4 bit processingRe = Integer.parseint (Bytestr, 2); }      return(byte) re; } 

2. Move left and right

The direct example illustrates:

(1) Move Left: 3 Move left 2 bit

  | 0000 0000 0000 0000 0000 0000 0000 0011

xx| 0000 0000 0000 0000 0000 0000 0000 One -off vacancy supplement 0

3<<1=6;3<<2=12;3<<3=24;

Thus a pattern can be seen:

3x2^1=6;3x2^2=12;3x2^3=24;

(2) Right Shift: 6 right Shift 2 bit

0000 0000 0000 0000 0000 0000 0000 0110|

xx0000 0000 0000 0000 0000 0000 0001| Ten

Vacancy 0 (see the highest, here the highest level is 0), negative number is 1, 1.

Summary:<< left Shift: is the number of bits multiplied by 2 to the power of the movement.

>> Right Shift: is the power of the number of bits divided by 2.

>>: The maximum number of bits is determined by the highest bit value of the original data, which is 0 or 1.

>>> unsigned Right Shift: no matter what the highest bit is, 0 is added after the right shift.

3, with (&), or (|), XOR (^)

The direct example illustrates:

(1) 6&3=2;

110 1 stands for true, 0 for false

 & 011

010=2

(2) 6|5=7;

110

  | 101

111=7

(3) 6^5=3;

110

^ 101

011=3

One more example: 7^4^4=7; (a number of different or the same number two times, the result is that number, can be used to encrypt data)

111

^

011

^

111=7

How to get the individual bit values of byte and common bit operations

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.