How do I get 4 bits from a 16 bit data?


Sajal Singh:

I use the getShort() method of the byte buffer to get my short value, I convert it to an int and print the binary value, which is a 16-bit data and I need to access from that 16-bit data 4-bit int value.

short bitRateShort = buf.getShort(32 + 2);
int bitRateInteger = Short.toUnsignedInt(bitRateShort);
System.out.println(bitRateInteger);//46144
System.out.println(Integer.toBinaryString(bitRateInteger));// 1011010001000000

I need to start with an integer value where 4 bits in my case are 1011, how can I mask these 16 bits to get 4 bits nibble?

AVI:

try this:

int bitRateInteger = 46144;
System.out.println(Integer.toBinaryString(bitRateInteger&0xF000));
System.out.println(Integer.toBinaryString((bitRateInteger&0xF000)>>12));

output:

1011000000000000
1011

First, we mask the starting 4 bits with bitmask 0xF000 (binary 0b1111_0000_0000_0000). Then we shift right by the bits we get by 12, getting rid of the "known" zeros at the end.

EDIT: As @CarlosHeuberger very kindly pointed out, you don't even need to do that &, since >>all right-hand bits are truncated by default. Well, do this instead:

int bitRateInteger = 46144;
System.out.println(Integer.toBinaryString(bitRateInteger>>12));

output:

1011

Related


How do I get 4 bits from a 16 bit data?

Sajal Singh: I use the getShort() method of the bytebuffer to get my short value, I convert it to an int and print the binary value, it's a 16 bit data that I need to access from that 16 bit data 4-bit int value. short bitRateShort = buf.getShort(32 + 2); int

How to get PCM 16-bit data from TarsosDSP AudioDispatcher?

Kubaib Ahmed| I want to get PCM 16bit data from TarsosDSP AudioDispatcher. I followed this link for pitch analysis of a live audio stream. I get the desired result, but I also want to get PCM data from AudioDispatcher. AudioDispatcher adp = AudioDispatcherFact

How to get PCM 16-bit data from TarsosDSP AudioDispatcher?

Kubaib Ahmed| I want to get PCM 16bit data from TarsosDSP AudioDispatcher. I followed this link for pitch analysis of a live audio stream. I get the desired result, but I also want to get PCM data from AudioDispatcher. AudioDispatcher adp = AudioDispatcherFact

How to get PCM 16-bit data from TarsosDSP AudioDispatcher?

Kubaib Ahmed| I want to get PCM 16bit data from TarsosDSP AudioDispatcher. I followed this link for pitch analysis of a live audio stream. I get the desired result, but I also want to get PCM data from AudioDispatcher. AudioDispatcher adp = AudioDispatcherFact

How to get PCM 16-bit data from TarsosDSP AudioDispatcher?

Kubaib Ahmed| I want to get PCM 16bit data from TarsosDSP AudioDispatcher. I followed this link for pitch analysis of a live audio stream. I get the desired result, but I also want to get PCM data from AudioDispatcher. AudioDispatcher adp = AudioDispatcherFact

How to get PCM 16-bit data from TarsosDSP AudioDispatcher?

Kubaib Ahmed| I want to get PCM 16bit data from TarsosDSP AudioDispatcher. I followed this link for pitch analysis of a live audio stream. I get the desired result, but I also want to get PCM data from AudioDispatcher. AudioDispatcher adp = AudioDispatcherFact

How can I get a value larger than 8 bits from an 8 bit integer?

unsigned I found an extremely nasty bug hiding behind this little gem. I know that signed overflow is undefined behavior according to the C++ spec, but overflow only happens when the value expands to bit-width sizeof(int). As far as I understand charit is neve

How can I get a value larger than 8 bits from an 8 bit integer?

unsigned I found an extremely nasty bug hiding behind this little gem. I know that signed overflow is undefined behavior according to the C++ spec, but overflow only happens when the value expands to bit-width sizeof(int). As far as I understand charit is neve

How can I get a value larger than 8 bits from an 8 bit integer?

unsigned I found an extremely nasty bug hiding behind this little gem. I know that signed overflow is undefined behavior according to the C++ spec, but overflow only happens when the value expands to bit-width sizeof(int). As far as I understand charit is neve

How to get first 16 bits from byteArray in Android Java?

Pooja Singh: Can anyone give me a util method that will give me the first 16 bits from a byte array in Android/Java? code: //Decode API response using Base64 decoder byte[] decodedResult = Base64.decode(apiResponse, Base64.DEFAULT); /

How to get first 16 bits from byteArray in Android Java?

Pooja Singh: Can anyone give me a util method that will give me the first 16 bits from a byte array in Android/Java? code: //Decode API response using Base64 decoder byte[] decodedResult = Base64.decode(apiResponse, Base64.DEFAULT); /

How to get first 16 bits from byteArray in Android Java?

Pooja Singh: Can anyone give me a util method that will give me the first 16 bits from a byte array in Android/Java? code: //Decode API response using Base64 decoder byte[] decodedResult = Base64.decode(apiResponse, Base64.DEFAULT); /

how do i get data from the table

Ruben Can someone assist me how to get data from a table in Excel 2013? I have a table called Personal: I want to have a control in a cell like this: (I don't know how to add this control in a cell) So I can select data from the table: I googled a lot before a

how do i get data from the table

Ruben Can someone assist me how to get data from a table in Excel 2013? I have a table called Personal: I want to have a control in a cell like this: (I don't know how to add this control in a cell) So I can select data from the table: I googled a lot before a

How to record sound with 16 bits per sample (16 bit depth)?

Eugenics I try to record PCM sound from flash (using microphone class). I use the org.bytearray.micrecorder.MicRecorder helper class. In the "Microphone" class, no properties like bitDepthor were found bitsPerSample. I always get 32 bit. is it possible? VC Upd

How do I load a 16-bit image into a Metal texture?

super junction The recommended usage MTKTextureLoader.newTexturedoes not work with 16-bit images. This named:version silently converts the image to 8-bit pixel format This cgImage:version is terminated withImage decoding failed Both UIImage and NSImage support

How do I load a 16-bit image into a Metal texture?

super junction The recommended usage MTKTextureLoader.newTexturedoes not work with 16-bit images. This named:version silently converts the image to 8-bit pixel format This cgImage:version is terminated withImage decoding failed Both UIImage and NSImage support

How do I use 16 bit integer types?

Esterification Problem : http://codeforces.com/problemset/problem/486/A _ enter: 1000000000000000 output: -1 answer: 500000000000000 Inspector log: wrong answer 1st numbers differ - expected: '500000000000000', found: '-1' Code: int main() { int a, b =

How do I load a 16-bit image into a Metal texture?

super junction The recommended usage MTKTextureLoader.newTexturedoes not work with 16-bit images. This named:version silently converts the image to 8-bit pixel format This cgImage:version is terminated withImage decoding failed Both UIImage and NSImage support

How do I use 16 bit integer types?

Esterification Problem : http://codeforces.com/problemset/problem/486/A _ enter: 1000000000000000 output: -1 answer: 500000000000000 Inspector log: wrong answer 1st numbers differ - expected: '500000000000000', found: '-1' Code: int main() { int a, b =

how do i get the position of the drill bit

username I have a decimal number and need to convert it to binary and then find the position of one in that binary representation. The input is 5, its binary is 101, the output should be 1 3 Below is my code, only giving the output as 2I want to give the loca

how do i get the position of the drill bit

username I have a decimal number that I need to convert to binary and then find the position of one in that binary representation. The input is 5, its binary is 101, the output should be 1 3 Below is my code, only giving the output as 2I want to give the loca

how do i get the position of the drill bit

username I have a decimal number and need to convert it to binary and then find the position of one in that binary representation. The input is 5, its binary is 101, the output should be 1 3 Below is my code, only giving the output as 2I want to give the loca

how do i get the position of the drill bit

username I have a decimal number that I need to convert to binary and then find the position of one in that binary representation. The input is 5, its binary is 101, the output should be 1 3 Below is my code, only giving the output as 2I want to give the loca

how do i get the position of the drill bit

username I have a decimal number and need to convert it to binary and then find the position of one in that binary representation. The input is 5, its binary is 101, the output should be 1 3 Below is my code, only giving the output as 2I want to give the loca

Get the value of one bit from 32 bits

Jonathan Kittell: How can I apply a mask to get only one bit after shifting right? Does it depend on how many positions you pan to the right? In a 32-bit structure, I try to get the value of the 9th and 10th bits. x := uint32(11537664) 0000 0000 1011 0000 000

Get the value of one bit from 32 bits

Jonathan Kittell: How can I apply a mask to get only one bit after shifting right? Does it depend on how many positions you pan to the right? In a 32-bit structure, I try to get the value of the 9th and 10th bits. x := uint32(11537664) 0000 0000 1011 0000 000