Velocity Reviews > Java > bit operations and parity

bit operations and parity

RVic
Guest
Posts: n/a

 07-29-2009
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,

Lew
Guest
Posts: n/a

 07-29-2009
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
Guest
Posts: n/a

 07-29-2009
Lew,

So if I have, say, byte inby[i]...

On Jul 29, 12:15*pm, Lew <(E-Mail Removed)> 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
Guest
Posts: n/a

 07-29-2009
//so, say I am wanting to confirm 7 bit even parity....
byte payload = (byte) (mybytearray[i] & 0xFE);
byte parity = (byte) (mybytearray[i] & SOH);
boolean isEven = ((int)payload) % 2 == 0;
if ((isEven && (int)parity != 0)|| (!isEven && (int)parity == 0)){
// parity mismatch!
} else{
}

Is that typically then how this can be done

RVic
Guest
Posts: n/a

 07-29-2009
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.

Mayeul
Guest
Posts: n/a

 07-29-2009
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

RVic
Guest
Posts: n/a

 07-29-2009
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.

Lew
Guest
Posts: n/a

 07-29-2009
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

Roedy Green
Guest
Posts: n/a

 07-29-2009
On Wed, 29 Jul 2009 08:51:00 -0700 (PDT), RVic <(E-Mail Removed)>
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.
--
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
Guest
Posts: n/a

 07-29-2009
On Wed, 29 Jul 2009 09:36:03 -0700 (PDT), RVic <(E-Mail Removed)>
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
--
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.