Home > OS >  How to encode chars in 2-bits? in java
How to encode chars in 2-bits? in java

Time:02-21

DNA molecules are denoted by one of four values: A, C, G, or T. I need to convert a string of characters from A, C, G, and T to an array of bytes, encoding each of the characters with two bits.A with bits 00, C with bits 01, G with 10, and T with 11. I don't understand how to convert characters to 2 bits. I was trying to shift and mask, but got wrong result. At the very beginning, I check if there are characters in the line. Then i convert each character into a bit value and insert it into an array. When i insert ACGT, in the output i got 0 1 3 2. And here I have a problem, because I don’t understand how to convert the value to 2 bits.

Scanner text = new Scanner(System.in);
String str = text.nextLine();

if (str.contains("A") && str.contains("C") && str.contains("G") && str.contains("T")){
  System.out.println("");
}
else
{
  System.out.println("wrong command format");
} 

byte mas[] = str.getBytes();
System.out.println("String in byte array : "   Arrays.toString(mas));

for (int i = 0; i < mas.length; i  ){
  byte mask = 3;
  byte number = mas[i];
  byte result = (byte)((number >> 1) & mask); 
  System.out.println(result);
}
}

}

CodePudding user response:

I haven't tested this, but it may help you.

byte test = 69;
byte insert = 0b01;
byte index = 2;
final byte ones = 0b00000011;
//Clear out the data at specified index
test = (byte) (test & ~(ones << index));
//Insert data
test |= (byte) (insert << index);

It works as follows:

  1. Clear the 2 bits at the index in the byte (using bitwise AND).
  2. Insert the 2 data bits at the index in the byte using bitwise OR).

CodePudding user response:

It seems that you want to save the bits in a byte. The following example might give some ideas.

public class Main
{
    private static final int A = 0x00; // b00
    private static final int C = 0x01; // b01
    private static final int G = 0x02; // b10
    private static final int T = 0x03; // b11
    
    public static void main(String[] args) throws Exception
    {   
        byte store = 0;
        store = setByte(store, 0, A);
        store = setByte(store, 1, C);
        store = setByte(store, 2, G);
        store = setByte(store, 3, T);
        System.out.println(Integer.toBinaryString(store));
        //11111111111111111111111111100100
        System.out.println(getByte(store, 0)); //0
        System.out.println(getByte(store, 1)); //1
        System.out.println(getByte(store, 2)); //2
        System.out.println(getByte(store, 3)); //3
    }
    
    //Behavior :: Store "value" into "store".
    //Reminder :: Valid index 0 - 3. Valid value 0 - 3.
    private static byte setByte(byte store, int index, int value)
    {   
        store = (byte)(store & ~(0x3 << (2 * index)));
        return store |= (value & 0x3) << (2 * index);
    }

    private static byte getByte(byte store, int index)
    {
        return (byte)((store >> (2 * index)) & 0x3);
    }
}
  • Related