bit operations and parity

Discussion in 'Java' started by RVic, Jul 29, 2009.

  1. RVic

    RVic Guest

    If I have an inputstream, as a byte[], and I know that the stream is
    7E1 (7 bits with 1 parity bit, even parity) how do I break apart each
    byte in the byte array into the constituent 7 bits (as the ASCII code
    or integer representation) and the parity bit? Surely there must be
    some very efficient means for doing this? Thanks,
    RVic, Jul 29, 2009
    #1
    1. Advertising

  2. RVic

    Lew Guest

    RVic wrote:
    > If I have an inputstream, as a byte[], and I know that the stream is
    > 7E1 (7 bits with 1 parity bit, even parity) how do I break apart each
    > byte in the byte array into the constituent 7 bits (as the ASCII code
    > or integer representation) and the parity bit? Surely there must be
    > some very efficient means for doing this? Thanks,
    >


    Use the integral & (mask) operator.

    The operand (maskend?) to use depends on which bits you want to mask.

    E.g., for an input byte 'inby' with the upper 7 bits as payload and
    the lowest bit as parity:

    byte payload = (byte) (inby & 0xFE);
    byte parity = (byte) (inby & 0x01);

    --
    Lew
    Lew, Jul 29, 2009
    #2
    1. Advertising

  3. RVic

    RVic Guest

    Lew,

    So if I have, say, byte inby...



    On Jul 29, 12:15 pm, Lew <> wrote:
    > RVic wrote:
    > > If I have an inputstream, as a byte[], and I know that the stream is
    > > 7E1 (7 bits with 1 parity bit, even parity) how do I break apart each
    > > byte in the byte array into the constituent 7 bits (as the ASCII code
    > > or integer representation) and the parity bit? Surely there must be
    > > some very efficient means for doing this? Thanks,

    >
    > Use the integral & (mask) operator.
    >
    > The operand (maskend?) to use depends on which bits you want to mask.
    >
    > E.g., for an input byte 'inby' with the upper 7 bits as payload and
    > the lowest bit as parity:
    >
    >  byte payload = (byte) (inby & 0xFE);
    >  byte parity  = (byte) (inby & 0x01);
    >
    > --
    > Lew
    RVic, Jul 29, 2009
    #3
  4. RVic

    RVic Guest

    //so, say I am wanting to confirm 7 bit even parity....
    byte payload = (byte) (mybytearray & 0xFE);
    byte parity = (byte) (mybytearray & SOH);
    boolean isEven = ((int)payload) % 2 == 0;
    if ((isEven && (int)parity != 0)|| (!isEven && (int)parity == 0)){
    // parity mismatch!
    } else{
    // append((char) payload) to what I am reading
    }

    Is that typically then how this can be done
    RVic, Jul 29, 2009
    #4
  5. RVic

    RVic Guest

    Thanks Eric.

    So then if theParityInPlace != theParityAsZeroOrOne I would assume
    that it is not even parity? And if I am thus looking for even parity,
    then I must require:

    theParityInPlace == theParityAsZeroOrOne

    Do I understand this correctly? Thanks again.
    RVic, Jul 29, 2009
    #5
  6. RVic

    Mayeul Guest

    RVic wrote:
    > Thanks Eric.
    >
    > So then if theParityInPlace != theParityAsZeroOrOne I would assume
    > that it is not even parity? And if I am thus looking for even parity,
    > then I must require:
    >
    > theParityInPlace == theParityAsZeroOrOne
    >
    > Do I understand this correctly? Thanks again.


    Far from it.

    How about trying to understand what is in theParityInPlace and in
    theParityAsZeroOrOne *before* trying to use them in some way?

    Anyway, if all you want to do is to check for even parity, you need neither.
    All you need to do is count the number of 1's in a byte and check this
    number is even.

    (On a side note, I know of no direct way to do that in Java.

    boolean even = true;
    for(int i = 0; i < 8; i++) {
    if((theByte & (1 << i)) != 0) {
    even = !even;
    }
    }

    Maybe?)

    --
    Mayeul
    Mayeul, Jul 29, 2009
    #6
  7. RVic

    RVic Guest

    Well it appears from what I can discern that if

    if(((int)theParityInPlace % 2) == (int) theParityAsZeroOrOne)

    then it is even parity and it passes -- if I understand what is going
    on correctly.
    RVic, Jul 29, 2009
    #7
  8. RVic

    Lew Guest

    Mayeul wrote:
    > Anyway, if all you want to do is to check for even parity, you need neither.
    > All you need to do is count the number of 1's in a byte and check this
    > number is even.
    >
    > (On a side note, I know of no direct way to do that in Java.
    >
    > boolean even = true;
    > for(int i = 0; i < 8; i++) {
    >    if((theByte & (1 << i)) != 0) {
    >      even = !even;
    >    }
    >
    > }
    >


    Not quite direct, but:

    boolean evenParity =
    ((Integer.bitCount( theByte & 0xff ) & 0x01) == 0);

    --
    Lew
    Lew, Jul 29, 2009
    #8
  9. RVic

    Roedy Green Guest

    On Wed, 29 Jul 2009 08:51:00 -0700 (PDT), RVic <>
    wrote, quoted or indirectly quoted someone who said :

    >If I have an inputstream, as a byte[], and I know that the stream is
    >7E1 (7 bits with 1 parity bit, even parity) how do I break apart each
    >byte in the byte array into the constituent 7 bits (as the ASCII code
    >or integer representation) and the parity bit? Surely there must be
    >some very efficient means for doing this? Thanks,


    Shift, mask and test in a loop.
    see http://mindprod.com/jgloss/parity.html
    for sample code.
    --
    Roedy Green Canadian Mind Products
    http://mindprod.com

    The USA and Iraq are as mismatched combatants as Mike Tyson and a bag lady. Americans make themselves look ridiculous when they justify invading and occupying Iraq in self defence.
    Roedy Green, Jul 29, 2009
    #9
  10. RVic

    Roedy Green Guest

    On Wed, 29 Jul 2009 09:36:03 -0700 (PDT), RVic <>
    wrote, quoted or indirectly quoted someone who said :

    >boolean isEven = ((int)payload) % 2 == 0;


    oops. That just tests the low order bit.

    see http://mindprod.com/jgloss/parity.html
    --
    Roedy Green Canadian Mind Products
    http://mindprod.com

    The USA and Iraq are as mismatched combatants as Mike Tyson and a bag lady. Americans make themselves look ridiculous when they justify invading and occupying Iraq in self defence.
    Roedy Green, Jul 29, 2009
    #10
  11. RVic

    Lew Guest

    RVic <> wrote:
    > Well it appears from what I can discern that if
    >
    >  if(((int)theParityInPlace % 2)  == (int) theParityAsZeroOrOne)
    >
    > then it is even parity and it passes -- if I understand what is going
    > on correctly.


    Nope.

    You assume that bit 7 is the parity bit, which it might not be. It
    could be that bit 0 is the parity bit. Which bit is the parity bit?

    'theParityInPlace' as defined above could be either zero or one in bit
    7 only, with the lower seven bits always zero, so taking it mod 2 will
    always yield zero.

    Thus your expression reduces to

    (0 == theParityAsZeroOrOne)

    and a parity bit of zero or one could happen for either even parity or
    odd parity. So your proposed expression reveals less than nothing
    about parity.

    Review Eric Sosman's answers again, particularly the one where he
    explains what parity means.

    And take his advice.

    --
    Lew
    Lew, Jul 29, 2009
    #11
  12. RVic

    Ian Shef Guest

    Mayeul <> wrote in
    news:4a708393$0$9973$:

    > RVic wrote:

    <snip>
    > Anyway, if all you want to do is to check for even parity, you need
    > neither. All you need to do is count the number of 1's in a byte and
    > check this number is even.
    >
    > (On a side note, I know of no direct way to do that in Java.
    >
    > boolean even = true;
    > for(int i = 0; i < 8; i++) {
    > if((theByte & (1 << i)) != 0) {
    > even = !even;
    > }
    > }
    >
    > Maybe?)

    Or a table lookup could be used. There are only 256 values in the table.
    The table could be computed offline and stored in the source code, or the
    table could be computed at initialization (perhaps by using the counting
    routine that you showed above). Also, I suspect that there may be faster
    ways to count. Depending upon how the jvm is implemented, having (1 << i) in
    the loop may be inefficent. It may be more efficient to do
    if ((theByte & 1) != 0)
    as the test, and
    theByte >>= 1 ;
    for the shift at the end of each iteration. Only one shift is needed per
    iteration, so there are no questions about whether the processor has a barrel
    shifter.

    I realize that you were not trying for speed, and that I have now drifted
    well off topic.


    >
    > --
    > Mayeul
    Ian Shef, Jul 29, 2009
    #12
  13. RVic

    Arne Vajhøj Guest

    Lew wrote:
    > RVic wrote:
    >> If I have an inputstream, as a byte[], and I know that the stream is
    >> 7E1 (7 bits with 1 parity bit, even parity) how do I break apart each
    >> byte in the byte array into the constituent 7 bits (as the ASCII code
    >> or integer representation) and the parity bit? Surely there must be
    >> some very efficient means for doing this? Thanks,

    >
    > Use the integral & (mask) operator.
    >
    > The operand (maskend?) to use depends on which bits you want to mask.
    >
    > E.g., for an input byte 'inby' with the upper 7 bits as payload and
    > the lowest bit as parity:
    >
    > byte payload = (byte) (inby & 0xFE);
    > byte parity = (byte) (inby & 0x01);


    byte payload = (byte) (inby & 0x7F);
    byte parity = (byte) ((inby >> 7)& 0x01);

    seems more likely.

    Arne
    Arne Vajhøj, Jul 29, 2009
    #13
  14. RVic

    Arne Vajhøj Guest

    Lew wrote:
    > Mayeul wrote:
    >> Anyway, if all you want to do is to check for even parity, you need neither.
    >> All you need to do is count the number of 1's in a byte and check this
    >> number is even.
    >>
    >> (On a side note, I know of no direct way to do that in Java.
    >>
    >> boolean even = true;
    >> for(int i = 0; i < 8; i++) {
    >> if((theByte & (1 << i)) != 0) {
    >> even = !even;
    >> }
    >>
    >> }

    >
    > Not quite direct, but:
    >
    > boolean evenParity =
    > ((Integer.bitCount( theByte & 0xff ) & 0x01) == 0);


    Or % 2 instead of & 0x01 if that sounds more "even".

    Arne
    Arne Vajhøj, Jul 29, 2009
    #14
  15. RVic

    Arne Vajhøj Guest

    Lew wrote:
    > RVic <> wrote:
    >> Well it appears from what I can discern that if
    >>
    >> if(((int)theParityInPlace % 2) == (int) theParityAsZeroOrOne)
    >>
    >> then it is even parity and it passes -- if I understand what is going
    >> on correctly.

    >
    > Nope.
    >
    > You assume that bit 7 is the parity bit, which it might not be. It
    > could be that bit 0 is the parity bit. Which bit is the parity bit?


    For ASCII data it would usually be the high bit.

    Arne
    Arne Vajhøj, Jul 29, 2009
    #15
  16. RVic

    Lew Guest

    Lew wrote:
    >> The operand (maskend?) to use depends on which bits you want to mask.

    >
    >> E.g., for an input byte 'inby' with the upper 7 bits as payload and
    >> the lowest bit as parity:

    >
    >>  byte payload = (byte) (inby & 0xFE);
    >>  byte parity  = (byte) (inby & 0x01);

    >


    Arne Vajhøj <> wrote:
    > byte payload = (byte) (inby & 0x7F);
    > byte parity  = (byte) ((inby >> 7)& 0x01);
    >
    > seems more likely.
    >


    That violates my explicitly stated assumption of "an input ...
    with ... the lowest bit as parity", but is a good solution for
    "highest bit as parity". The OP didn't say which one applies.

    There certainly are transmission schemes where the parity bit(s) follow
    (s) the data bits. The OP will need to know whether parity preceding
    or parity following, if indeed either one, applies and adjust their
    calculations accordingly.

    --
    Lew
    Lew, Jul 29, 2009
    #16
  17. RVic

    Lew Guest

    On Jul 29, 4:09 pm, Arne Vajhøj <> wrote:
    > Lew wrote:
    > > RVic <> wrote:
    > >> Well it appears from what I can discern that if

    >
    > >>  if(((int)theParityInPlace % 2)  == (int) theParityAsZeroOrOne)

    >
    > >> then it is even parity and it passes -- if I understand what is going
    > >> on correctly.

    >
    > > Nope.

    >
    > > You assume that bit 7 is the parity bit, which it might not be.  It
    > > could be that bit 0 is the parity bit.  Which bit is the parity bit?

    >
    > For ASCII data it would usually be the high bit.


    Even so, the rest of the OP's analysis was incorrect.

    --
    Lew
    Lew, Jul 29, 2009
    #17
  18. RVic

    Lew Guest

    Please do not top-post.

    RVic wrote:
    > So if I have, say, byte inby...


    Same thing but with the particular array element.

    --
    Lew
    Lew, Jul 30, 2009
    #18
  19. RVic

    Arne Vajhøj Guest

    Lew wrote:
    > On Jul 29, 4:09 pm, Arne Vajhøj <> wrote:
    >> Lew wrote:
    >>> RVic <> wrote:
    >>>> Well it appears from what I can discern that if
    >>>> if(((int)theParityInPlace % 2) == (int) theParityAsZeroOrOne)
    >>>> then it is even parity and it passes -- if I understand what is going
    >>>> on correctly.
    >>> Nope.
    >>> You assume that bit 7 is the parity bit, which it might not be. It
    >>> could be that bit 0 is the parity bit. Which bit is the parity bit?

    >> For ASCII data it would usually be the high bit.

    >
    > Even so, the rest of the OP's analysis was incorrect.


    True.

    Arne
    Arne Vajhøj, Jul 30, 2009
    #19
  20. RVic

    Arne Vajhøj Guest

    Lew wrote:
    > Lew wrote:
    >>> The operand (maskend?) to use depends on which bits you want to mask.
    >>> E.g., for an input byte 'inby' with the upper 7 bits as payload and
    >>> the lowest bit as parity:
    >>> byte payload = (byte) (inby & 0xFE);
    >>> byte parity = (byte) (inby & 0x01);

    >
    > Arne Vajhøj <> wrote:
    >> byte payload = (byte) (inby & 0x7F);
    >> byte parity = (byte) ((inby >> 7)& 0x01);
    >>
    >> seems more likely.
    >>

    >
    > That violates my explicitly stated assumption of "an input ...
    > with ... the lowest bit as parity", but is a good solution for
    > "highest bit as parity". The OP didn't say which one applies.
    >
    > There certainly are transmission schemes where the parity bit(s) follow
    > (s) the data bits. The OP will need to know whether parity preceding
    > or parity following, if indeed either one, applies and adjust their
    > calculations accordingly.


    He did say ASCII.

    And ASCII has the data in the low 7 bit.

    Arne

    PS: Or should we call it US-ASCII ... :) :) :)
    Arne Vajhøj, Jul 30, 2009
    #20
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Ed

    Parity Check

    Ed, Sep 30, 2004, in forum: VHDL
    Replies:
    18
    Views:
    10,771
    yassen
    Oct 6, 2009
  2. Jesus M. Salvo Jr.
    Replies:
    2
    Views:
    4,032
    robert
    Feb 11, 2006
  3. snacktime

    Set parity of a string

    snacktime, Jan 23, 2005, in forum: Python
    Replies:
    8
    Views:
    375
    Peter Hansen
    Jan 26, 2005
  4. Petr Jakes
    Replies:
    2
    Views:
    5,058
    Petr Jakes
    Nov 12, 2005
  5. shakeelsultan

    odd parity checker FSM

    shakeelsultan, Oct 27, 2006, in forum: VHDL
    Replies:
    0
    Views:
    797
    shakeelsultan
    Oct 27, 2006
Loading...

Share This Page