bit operations and parity

R

RVic

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,
 
L

Lew

RVic 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,

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);
 
R

RVic

Lew,

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



RVic 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,

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);
 
R

RVic

//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
 
R

RVic

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.
 
M

Mayeul

RVic said:
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?)
 
R

RVic

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.
 
L

Lew

Mayeul said:
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);
 
R

Roedy Green

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.
 
L

Lew

RVic said:
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.
 
I

Ian Shef

RVic 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;
}
}

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.
 
A

Arne Vajhøj

Lew said:
RVic 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,

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
 
A

Arne Vajhøj

Lew said:
Not quite direct, but:

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

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

Arne
 
A

Arne Vajhøj

Lew said:
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
 
L

Lew

Arne Vajhøj said:
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.
 
A

Arne Vajhøj

Lew said:
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 ... :) :) :)
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,054
Latest member
TrimKetoBoost

Latest Threads

Top