neha
neha

Reputation: 27

How do I divide a string of 64bits into four 16bits in Java?

How can I split the 64 bit input into 16 bits?

Upvotes: 1

Views: 1262

Answers (4)

Toad
Toad

Reputation: 15925

given that value contains your 64bit number

 char a1 = (char) (value & 0xffff);
 char a2 = (char) ((value>>16) & 0xffff);
 char a3 = (char) ((value>>32) & 0xffff);
 char a4 = (char) ((value>>48) & 0xffff);

edit: added casts as suggested by commenter

Upvotes: 0

vicatcu
vicatcu

Reputation: 5837

Here's an inappropriately generic way of doing it :):

public class MyClass

{ 

    static public long[] splitIntoNBits(long value, int numBitsPerChunk){      

        long[] retVal = null;

        if(numBitsPerChunk == 64){
            retVal = new long[1];
            retVal[0] = value;
            return retVal;
        }

        if(numBitsPerChunk <= 0 || numBitsPerChunk > 64){
            return null;
        }

        long mask = (1 << numBitsPerChunk) - 1;      
        int numFullChunks = (byte) (64 / numBitsPerChunk);
        int numBitsInLastChunk = (byte) (64 - numFullChunks * numBitsPerChunk);
        int numTotalChunks = numFullChunks;
        if(numBitsInLastChunk > 0){
            numTotalChunks++;
        }

        retVal = new long[numTotalChunks];

        for(int i = 0; i < numTotalChunks; i++){
            retVal[i] = value & mask;
            value >>= numBitsPerChunk;
        }

        // clean up the last chunk
        if(numBitsInLastChunk > 0){
            mask = (1 << numBitsInLastChunk) - 1;
            retVal[retVal.length - 1] &= mask;
        }

        return retVal;

    }       




    public static void main(String[] args) 
    {       
        long myvalue = ((long) 0x12345678) | (((long) 0xABCDEF99) << 32);       
        long[] bitFields = splitIntoNBits(myvalue, 16);
        for(int i=0; i < bitFields.length; i++){
            System.out.printf("Field %d: %x\r\n", i, bitFields[i]);
        }
    } 
}

produces output:

Field 0: 5678
Field 1: 1234
Field 2: ef99
Field 3: abcd

and for a bitsPerField of 7 it produces:

Field 0: 78
Field 1: 2c
Field 2: 51
Field 3: 11
Field 4: 11
Field 5: 73
Field 6: 7b
Field 7: 66
Field 8: 2b
Field 9: 1

Isn't that fun!?

Upvotes: 1

finnw
finnw

Reputation: 48629

Using a ByteBuffer:

ByteBuffer buf = ByteBuffer.allocate(Long.SIZE / Byte.SIZE);
buf.asLongBuffer().put(0, value);
short a0 = buf.asShortBuffer().get(0);
short a1 = buf.asShortBuffer().get(1);
short a2 = buf.asShortBuffer().get(2);
short a3 = buf.asShortBuffer().get(3);

Upvotes: 3

jjnguy
jjnguy

Reputation: 138884

String s = someStringOfBits;
String s0 = someStringOfbits.substring(0, 16);
String s1 = someStringOfbits.substring(16, 32);
String s2 = someStringOfbits.substring(32, 48);
String s3 = someStringOfbits.substring(48, 64);

This is assuming you actually have a string that looks similar to this:

1010101010101010101010101010101010101010101010101010101010101010

Upvotes: 4

Related Questions