# bit operations and parity

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

1. ### RVicGuest

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

2. ### LewGuest

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

3. ### RVicGuest

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
4. ### RVicGuest

//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{
}

Is that typically then how this can be done

RVic, Jul 29, 2009
5. ### RVicGuest

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
6. ### MayeulGuest

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
7. ### RVicGuest

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
8. ### LewGuest

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
9. ### Roedy GreenGuest

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.
--
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. ### Roedy GreenGuest

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
--
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
11. ### LewGuest

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.

(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

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

--
Lew

Lew, Jul 29, 2009
12. ### Ian ShefGuest

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
13. ### Arne VajhøjGuest

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
14. ### Arne VajhøjGuest

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
15. ### Arne VajhøjGuest

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
16. ### LewGuest

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
17. ### LewGuest

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
18. ### LewGuest

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

Same thing but with the particular array element.

--
Lew

Lew, Jul 30, 2009
19. ### Arne VajhøjGuest

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
20. ### Arne VajhøjGuest

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