472,805 Members | 948 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 472,805 software developers and data experts.

Extracting nibbles

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
6 9900
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
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
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
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
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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

5
by: Nazgul | last post by:
Hi! I want to implement a small tool in Python for distributing "patches" and I need Your advice. This application should be able to package all files chosen by a user into a self-extracting.exe...
10
by: Calvin FONG | last post by:
Dear all, Are there any utility that can be call by python to create self extracting zip file. I'm now using the powerarchiever. But the command line options aren't flexible enough. Basically, I...
2
by: Avi | last post by:
hi, Can anyone tell me what the problem is and how to solve it The following piece of code resides on an asp page on the server and is used to download files from the server to the machine...
4
by: Sreejith K | last post by:
How to interchange the two nibbles of a byte without using any bitwise operator.
5
by: Michael Hill | last post by:
Hi, folks. I am writing a Javascript program that accepts (x, y) data pairs from a text box and then analyzes that data in various ways. This is my first time using text area boxes; in the past,...
1
by: Cognizance | last post by:
Hi gang, I'm an ASP developer by trade, but I've had to create client side scripts with JavaScript many times in the past. Simple things, like validating form elements and such. Now I've been...
0
by: k_nil | last post by:
I have a link on my web page for a self extracting executable file placed on the server. When the link is clicked, 1) i could see dialog box with open or save options 2) when open clicked, self...
2
by: bjm | last post by:
I created a self extracting zip file with about 9000 files in it. I extracted it manually from the command line without a problem. However, when I tried to do the same extraction at the same...
6
by: Werner | last post by:
Hi, I try to read (and extract) some "self extracting" zipefiles on a Windows system. The standard module zipefile seems not to be able to handle this. False Is there a wrapper or has...
0
linyimin
by: linyimin | last post by:
Spring Startup Analyzer generates an interactive Spring application startup report that lets you understand what contributes to the application startup time and helps to optimize it. Support for...
0
by: erikbower65 | last post by:
Here's a concise step-by-step guide for manually installing IntelliJ IDEA: 1. Download: Visit the official JetBrains website and download the IntelliJ IDEA Community or Ultimate edition based on...
2
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Sept 2023 starting at 18:00 UK time (6PM UTC+1) and finishing at about 19:15 (7.15PM) The start time is equivalent to 19:00 (7PM) in Central...
0
by: Taofi | last post by:
I try to insert a new record but the error message says the number of query names and destination fields are not the same This are my field names ID, Budgeted, Actual, Status and Differences ...
14
DJRhino1175
by: DJRhino1175 | last post by:
When I run this code I get an error, its Run-time error# 424 Object required...This is my first attempt at doing something like this. I test the entire code and it worked until I added this - If...
5
by: DJRhino | last post by:
Private Sub CboDrawingID_BeforeUpdate(Cancel As Integer) If = 310029923 Or 310030138 Or 310030152 Or 310030346 Or 310030348 Or _ 310030356 Or 310030359 Or 310030362 Or...
0
by: lllomh | last post by:
How does React native implement an English player?
0
by: Mushico | last post by:
How to calculate date of retirement from date of birth
2
by: DJRhino | last post by:
Was curious if anyone else was having this same issue or not.... I was just Up/Down graded to windows 11 and now my access combo boxes are not acting right. With win 10 I could start typing...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.