Extracting nibbles

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Hugh Lutley

    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)

  • Kai Grossjohann

    #2
    Re: Extracting nibbles

    Hugh Lutley <hugh@richieDEL ELTETHISBITkotz en.com> writes:
    [color=blue]
    > 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???[/color]

    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

    Comment

    • Hugh Lutley

      #3
      Re: Extracting nibbles

      On Tue, 03 Feb 2004 16:55:35 +0100, Kai Grossjohann wrote:
      [color=blue]
      > Hugh Lutley <hugh@richieDEL ELTETHISBITkotz en.com> writes:
      >[color=green]
      >> 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???[/color]
      >
      > Byte modulo 16 is the low nibble, byte div 16 is the high nibble.
      >[/color]
      That is what I did in the first place but the timecodes came out very
      wrong.

      Hmmm..

      Hugh

      [color=blue]
      > 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[/color]

      --
      /* 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)

      Comment

      • nos

        #4
        Re: Extracting nibbles


        "Hugh Lutley" <hugh@richieDEL ELTETHISBITkotz en.com> wrote in message
        news:pan.2004.0 2.03.17.53.51.4 6359@richieDELE LTETHISBITkotze n.com...[color=blue]
        > On Tue, 03 Feb 2004 16:55:35 +0100, Kai Grossjohann wrote:
        >[color=green]
        > > Hugh Lutley <hugh@richieDEL ELTETHISBITkotz en.com> writes:
        > >[color=darkred]
        > >> I'm writing a Java prog that communticates over rs232 to tape machines[/color][/color][/color]
        and[color=blue][color=green][color=darkred]
        > >> 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[/color][/color][/color]
        etc.[color=blue][color=green][color=darkred]
        > >> The upper nibble counts 10's and the lower nibble counts 1's. What[/color][/color][/color]
        would[color=blue][color=green][color=darkred]
        > >> be a good way of accessing and reading the nibbles???[/color]
        > >
        > > Byte modulo 16 is the low nibble, byte div 16 is the high nibble.
        > >[/color]
        > That is what I did in the first place but the timecodes came out very
        > wrong.[/color]

        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;
        [color=blue]
        >
        > Hmmm..
        >
        > Hugh
        >
        >[color=green]
        > > 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[/color]
        >
        > --
        > /* 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)
        >[/color]


        Comment

        • Hugh Lutley

          #5
          Re: Extracting nibbles

          On Tue, 03 Feb 2004 22:46:31 +0000, nos wrote:
          [color=blue]
          >
          > "Hugh Lutley" <hugh@richieDEL ELTETHISBITkotz en.com> wrote in message
          > news:pan.2004.0 2.03.17.53.51.4 6359@richieDELE LTETHISBITkotze n.com...[color=green]
          >> On Tue, 03 Feb 2004 16:55:35 +0100, Kai Grossjohann wrote:
          >>[color=darkred]
          >> > Hugh Lutley <hugh@richieDEL ELTETHISBITkotz en.com> writes:
          >> >
          >> >> I'm writing a Java prog that communticates over rs232 to tape machines[/color][/color]
          > and[color=green][color=darkred]
          >> >> 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[/color][/color]
          > etc.[color=green][color=darkred]
          >> >> The upper nibble counts 10's and the lower nibble counts 1's. What[/color][/color]
          > would[color=green][color=darkred]
          >> >> be a good way of accessing and reading the nibbles???
          >> >
          >> > Byte modulo 16 is the low nibble, byte div 16 is the high nibble.
          >> >[/color]
          >> That is what I did in the first place but the timecodes came out very
          >> wrong.[/color]
          >
          > 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;
          >[/color]

          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[color=blue][color=green]
          >>
          >> Hmmm..
          >>
          >> Hugh
          >>
          >>[color=darkred]
          >> > 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[/color]
          >>
          >> --
          >> /* 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)
          >>[/color][/color]

          --
          /* 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)

          Comment

          • nos

            #6
            Re: Extracting nibbles


            "Hugh Lutley" <hugh@richieDEL ELTETHISBITkotz en.com> wrote in message
            news:pan.2004.0 2.03.23.05.08.1 96848@richieDEL ELTETHISBITkotz en.com...[color=blue]
            > On Tue, 03 Feb 2004 22:46:31 +0000, nos wrote:
            >[color=green]
            > >
            > > "Hugh Lutley" <hugh@richieDEL ELTETHISBITkotz en.com> wrote in message
            > > news:pan.2004.0 2.03.17.53.51.4 6359@richieDELE LTETHISBITkotze n.com...[color=darkred]
            > >> On Tue, 03 Feb 2004 16:55:35 +0100, Kai Grossjohann wrote:
            > >>
            > >> > Hugh Lutley <hugh@richieDEL ELTETHISBITkotz en.com> writes:
            > >> >
            > >> >> I'm writing a Java prog that communticates over rs232 to tape[/color][/color][/color]
            machines[color=blue][color=green]
            > > and[color=darkred]
            > >> >> I've got a problem which I'm not sure how to solve. My program[/color][/color][/color]
            recieves[color=blue][color=green][color=darkred]
            > >> >> timecode information over four bytes, one for hours, one for minutes[/color]
            > > etc.[color=darkred]
            > >> >> The upper nibble counts 10's and the lower nibble counts 1's. What[/color]
            > > would[color=darkred]
            > >> >> 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.[/color]
            > >
            > > 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;
            > >[/color]
            >
            > 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??[/color]

            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[color=blue]
            >
            > Cheers[color=green][color=darkred]
            > >>
            > >> 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)
            > >>[/color][/color]
            >
            > --
            > /* 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)
            >[/color]


            Comment

            • Hugh Lutley

              #7
              Re: Extracting nibbles

              On Tue, 03 Feb 2004 23:34:07 +0000, nos wrote:
              [color=blue]
              >
              > "Hugh Lutley" <hugh@richieDEL ELTETHISBITkotz en.com> wrote in message
              > news:pan.2004.0 2.03.23.05.08.1 96848@richieDEL ELTETHISBITkotz en.com...[color=green]
              >> On Tue, 03 Feb 2004 22:46:31 +0000, nos wrote:
              >>[color=darkred]
              >> >
              >> > "Hugh Lutley" <hugh@richieDEL ELTETHISBITkotz en.com> wrote in message
              >> > news:pan.2004.0 2.03.17.53.51.4 6359@richieDELE LTETHISBITkotze n.com...
              >> >> On Tue, 03 Feb 2004 16:55:35 +0100, Kai Grossjohann wrote:
              >> >>
              >> >> > Hugh Lutley <hugh@richieDEL ELTETHISBITkotz en.com> writes:
              >> >> >
              >> >> >> I'm writing a Java prog that communticates over rs232 to tape[/color][/color]
              > machines[color=green][color=darkred]
              >> > and
              >> >> >> I've got a problem which I'm not sure how to solve. My program[/color][/color]
              > recieves[color=green][color=darkred]
              >> >> >> 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;
              >> >[/color]
              >>
              >> 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??[/color]
              >
              > 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[/color]

              Thanks very much for that, I'll code it in this afternoon and check it out
              into a vt at the weekend.
              [color=blue][color=green]
              >>
              >> Cheers[color=darkred]
              >> >>
              >> >> 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)
              >> >>
              >> >>[/color]
              >> --
              >> /* 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)
              >>[/color][/color]

              --
              /* 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)

              Comment

              Working...