By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
428,558 Members | 1,603 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 428,558 IT Pros & Developers. It's quick & easy.

Extracting nibbles

P: n/a
I'm not much of a programmer so I need a bit of help on this one.

I'm writing a Java prog that communticates over rs232 to tape machines and
I've got a problem which I'm not sure how to solve. My program recieves
timecode information over four bytes, one for hours, one for minutes etc.
The upper nibble counts 10's and the lower nibble counts 1's. What would
be a good way of accessing and reading the nibbles???

My guess is that the upper nibble could be shifted four but for the lower
nibble I'm not sure.

--
/* Hugh Lutley aKa Spewy
* This message was created on either
* Mandrake 9.2 Linux on Athlon XP <- Gnome 2.4 ->
* Slackware 9.1 Linux on P166 <- Command Line Zone ->
*/
(created in ViM)

Jul 17 '05 #1
Share this Question
Share on Google+
6 Replies


P: n/a
Hugh Lutley <hu**@richieDELELTETHISBITkotzen.com> writes:
I'm writing a Java prog that communticates over rs232 to tape machines and
I've got a problem which I'm not sure how to solve. My program recieves
timecode information over four bytes, one for hours, one for minutes etc.
The upper nibble counts 10's and the lower nibble counts 1's. What would
be a good way of accessing and reading the nibbles???


Byte modulo 16 is the low nibble, byte div 16 is the high nibble.

This assume that you have individual bytes. Maybe you have a
four-byte integer, then you need to extract the bytes. The low byte
is integer mod 256, the next higher byte is (integer div 256) mod
256, the next one is ((integer div 256) div 256) mod 256, and the
high byte is ((integer div 256) div 256) div 256.

Kai
Jul 17 '05 #2

P: n/a
On Tue, 03 Feb 2004 16:55:35 +0100, Kai Grossjohann wrote:
Hugh Lutley <hu**@richieDELELTETHISBITkotzen.com> writes:
I'm writing a Java prog that communticates over rs232 to tape machines and
I've got a problem which I'm not sure how to solve. My program recieves
timecode information over four bytes, one for hours, one for minutes etc.
The upper nibble counts 10's and the lower nibble counts 1's. What would
be a good way of accessing and reading the nibbles???
Byte modulo 16 is the low nibble, byte div 16 is the high nibble.

That is what I did in the first place but the timecodes came out very
wrong.

Hmmm..

Hugh

This assume that you have individual bytes. Maybe you have a
four-byte integer, then you need to extract the bytes. The low byte
is integer mod 256, the next higher byte is (integer div 256) mod
256, the next one is ((integer div 256) div 256) mod 256, and the
high byte is ((integer div 256) div 256) div 256.

Kai


--
/* Hugh Lutley aKa Spewy
* This message was created on either
* Mandrake 9.2 Linux on Athlon XP <- Gnome 2.4 ->
* Slackware 9.1 Linux on P166 <- Command Line Zone ->
*/
(created in ViM)

Jul 17 '05 #3

P: n/a
nos

"Hugh Lutley" <hu**@richieDELELTETHISBITkotzen.com> wrote in message
news:pa***************************@richieDELELTETH ISBITkotzen.com...
On Tue, 03 Feb 2004 16:55:35 +0100, Kai Grossjohann wrote:
Hugh Lutley <hu**@richieDELELTETHISBITkotzen.com> writes:
I'm writing a Java prog that communticates over rs232 to tape machines and I've got a problem which I'm not sure how to solve. My program recieves
timecode information over four bytes, one for hours, one for minutes etc. The upper nibble counts 10's and the lower nibble counts 1's. What would be a good way of accessing and reading the nibbles???
Byte modulo 16 is the low nibble, byte div 16 is the high nibble.

That is what I did in the first place but the timecodes came out very
wrong.


In java byte is a signed integer with range -128 to 127.
When you divide a negative integer by 16 you get a negative result.
A nibble is 4 bits, not 8.
Try something like this:
int hour = x; // assume x is your value from the serial port
int hh = hour & 0xff;

Hmmm..

Hugh

This assume that you have individual bytes. Maybe you have a
four-byte integer, then you need to extract the bytes. The low byte
is integer mod 256, the next higher byte is (integer div 256) mod
256, the next one is ((integer div 256) div 256) mod 256, and the
high byte is ((integer div 256) div 256) div 256.

Kai


--
/* Hugh Lutley aKa Spewy
* This message was created on either
* Mandrake 9.2 Linux on Athlon XP <- Gnome 2.4 ->
* Slackware 9.1 Linux on P166 <- Command Line Zone ->
*/
(created in ViM)

Jul 17 '05 #4

P: n/a
On Tue, 03 Feb 2004 22:46:31 +0000, nos wrote:

"Hugh Lutley" <hu**@richieDELELTETHISBITkotzen.com> wrote in message
news:pa***************************@richieDELELTETH ISBITkotzen.com...
On Tue, 03 Feb 2004 16:55:35 +0100, Kai Grossjohann wrote:
> Hugh Lutley <hu**@richieDELELTETHISBITkotzen.com> writes:
>
>> I'm writing a Java prog that communticates over rs232 to tape machines and >> I've got a problem which I'm not sure how to solve. My program recieves
>> timecode information over four bytes, one for hours, one for minutes etc. >> The upper nibble counts 10's and the lower nibble counts 1's. What would >> be a good way of accessing and reading the nibbles???
>
> Byte modulo 16 is the low nibble, byte div 16 is the high nibble.
>

That is what I did in the first place but the timecodes came out very
wrong.


In java byte is a signed integer with range -128 to 127.
When you divide a negative integer by 16 you get a negative result.
A nibble is 4 bits, not 8.
Try something like this:
int hour = x; // assume x is your value from the serial port
int hh = hour & 0xff;


The byte I'm getting from the port is passed to me as an int. What does
the & 0xff actually do? Since I'm after the higher nibble and the lower
one too which nibble is this line of code for??

Cheers

Hmmm..

Hugh

> This assume that you have individual bytes. Maybe you have a
> four-byte integer, then you need to extract the bytes. The low byte
> is integer mod 256, the next higher byte is (integer div 256) mod
> 256, the next one is ((integer div 256) div 256) mod 256, and the
> high byte is ((integer div 256) div 256) div 256.
>
> Kai


--
/* Hugh Lutley aKa Spewy
* This message was created on either
* Mandrake 9.2 Linux on Athlon XP <- Gnome 2.4 -> * Slackware 9.1
Linux on P166 <- Command Line Zone ->
*/
(created in ViM)


--
/* Hugh Lutley aKa Spewy
* This message was created on either
* Mandrake 9.2 Linux on Athlon XP <- Gnome 2.4 ->
* Slackware 9.1 Linux on P166 <- Command Line Zone ->
*/
(created in ViM)

Jul 17 '05 #5

P: n/a
nos

"Hugh Lutley" <hu**@richieDELELTETHISBITkotzen.com> wrote in message
news:pa****************************@richieDELELTET HISBITkotzen.com...
On Tue, 03 Feb 2004 22:46:31 +0000, nos wrote:

"Hugh Lutley" <hu**@richieDELELTETHISBITkotzen.com> wrote in message
news:pa***************************@richieDELELTETH ISBITkotzen.com...
On Tue, 03 Feb 2004 16:55:35 +0100, Kai Grossjohann wrote:

> Hugh Lutley <hu**@richieDELELTETHISBITkotzen.com> writes:
>
>> I'm writing a Java prog that communticates over rs232 to tape machines
and
>> I've got a problem which I'm not sure how to solve. My program
recieves >> timecode information over four bytes, one for hours, one for minutes etc.
>> The upper nibble counts 10's and the lower nibble counts 1's. What

would
>> be a good way of accessing and reading the nibbles???
>
> Byte modulo 16 is the low nibble, byte div 16 is the high nibble.
>
That is what I did in the first place but the timecodes came out very
wrong.


In java byte is a signed integer with range -128 to 127.
When you divide a negative integer by 16 you get a negative result.
A nibble is 4 bits, not 8.
Try something like this:
int hour = x; // assume x is your value from the serial port
int hh = hour & 0xff;


The byte I'm getting from the port is passed to me as an int. What does
the & 0xff actually do? Since I'm after the higher nibble and the lower
one too which nibble is this line of code for??


int high = (hour >> 4) & 0x0f;
int low = hour & 0x0f;

the '&' is the 'and' operator
using & with 0x0f sets all bits to zero except the rightmost 4 bits
which are left alone
Cheers
Hmmm..

Hugh
> This assume that you have individual bytes. Maybe you have a
> four-byte integer, then you need to extract the bytes. The low byte
> is integer mod 256, the next higher byte is (integer div 256) mod
> 256, the next one is ((integer div 256) div 256) mod 256, and the
> high byte is ((integer div 256) div 256) div 256.
>
> Kai

--
/* Hugh Lutley aKa Spewy
* This message was created on either
* Mandrake 9.2 Linux on Athlon XP <- Gnome 2.4 -> * Slackware 9.1
Linux on P166 <- Command Line Zone ->
*/
(created in ViM)


--
/* Hugh Lutley aKa Spewy
* This message was created on either
* Mandrake 9.2 Linux on Athlon XP <- Gnome 2.4 ->
* Slackware 9.1 Linux on P166 <- Command Line Zone ->
*/
(created in ViM)

Jul 17 '05 #6

P: n/a
On Tue, 03 Feb 2004 23:34:07 +0000, nos wrote:

"Hugh Lutley" <hu**@richieDELELTETHISBITkotzen.com> wrote in message
news:pa****************************@richieDELELTET HISBITkotzen.com...
On Tue, 03 Feb 2004 22:46:31 +0000, nos wrote:
>
> "Hugh Lutley" <hu**@richieDELELTETHISBITkotzen.com> wrote in message
> news:pa***************************@richieDELELTETH ISBITkotzen.com...
>> On Tue, 03 Feb 2004 16:55:35 +0100, Kai Grossjohann wrote:
>>
>> > Hugh Lutley <hu**@richieDELELTETHISBITkotzen.com> writes:
>> >
>> >> I'm writing a Java prog that communticates over rs232 to tape machines > and
>> >> I've got a problem which I'm not sure how to solve. My program recieves >> >> timecode information over four bytes, one for hours, one for minutes
> etc.
>> >> The upper nibble counts 10's and the lower nibble counts 1's. What
> would
>> >> be a good way of accessing and reading the nibbles???
>> >
>> > Byte modulo 16 is the low nibble, byte div 16 is the high nibble.
>> >
>> That is what I did in the first place but the timecodes came out very
>> wrong.
>
> In java byte is a signed integer with range -128 to 127.
> When you divide a negative integer by 16 you get a negative result.
> A nibble is 4 bits, not 8.
> Try something like this:
> int hour = x; // assume x is your value from the serial port
> int hh = hour & 0xff;
>


The byte I'm getting from the port is passed to me as an int. What does
the & 0xff actually do? Since I'm after the higher nibble and the lower
one too which nibble is this line of code for??


int high = (hour >> 4) & 0x0f;
int low = hour & 0x0f;

the '&' is the 'and' operator
using & with 0x0f sets all bits to zero except the rightmost 4 bits
which are left alone


Thanks very much for that, I'll code it in this afternoon and check it out
into a vt at the weekend.

Cheers
>>
>> Hmmm..
>>
>> Hugh
>>
>>
>> > This assume that you have individual bytes. Maybe you have a
>> > four-byte integer, then you need to extract the bytes. The low
>> > byte is integer mod 256, the next higher byte is (integer div 256)
>> > mod 256, the next one is ((integer div 256) div 256) mod 256, and
>> > the high byte is ((integer div 256) div 256) div 256.
>> >
>> > Kai
>>
>> --
>> /* Hugh Lutley aKa Spewy
>> * This message was created on either
>> * Mandrake 9.2 Linux on Athlon XP <- Gnome 2.4 -> * Slackware 9.1
>> Linux on P166 <- Command Line Zone ->
>> */
>> (created in ViM)
>>
>>

--
/* Hugh Lutley aKa Spewy
* This message was created on either
* Mandrake 9.2 Linux on Athlon XP <- Gnome 2.4 -> * Slackware 9.1
Linux on P166 <- Command Line Zone ->
*/
(created in ViM)


--
/* Hugh Lutley aKa Spewy
* This message was created on either
* Mandrake 9.2 Linux on Athlon XP <- Gnome 2.4 ->
* Slackware 9.1 Linux on P166 <- Command Line Zone ->
*/
(created in ViM)

Jul 17 '05 #7

This discussion thread is closed

Replies have been disabled for this discussion.