A
A
amorphine2016-11-30 19:21:31
Programming
amorphine, 2016-11-30 19:21:31

Method for writing nibble to byte array - ideas?

There is an object represented by two id: id_a and id_b.
id_a - id one byte long.
id_b - id one nibble long.
There are two arrays: byte[4096] and byte[2048].
The first array stores byte id_a, 4096 IDs in total. It corresponds to the second array with nibbles (also 4096 nibbles) that fit in a byte array of length 2048.
Knowing the offset for id_a, we can read id_b:

public static byte Nibble4(byte[] arr, int offset){
        return (byte) (offset%2 == 0 ? arr[offset/2]&0x0F : (arr[offset/2]>>4)&0x0F);
}

where arr is an array with nibbles.
Question - how can I write a nibble, having an array with nibbles and an offset? Maybe pseudocode. Thank you.

Answer the question

In order to leave comments, you need to log in

1 answer(s)
R
Rsa97, 2016-11-30
@amorphine

shift = (offset % 2) << 2;
arr[offset/2] = (arr[offset/2] & (0xF0 >> shift)) | ((value &0xF) << shift);

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question