Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Java > bit operations and parity

Reply
Thread Tools

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,
 
Reply With Quote
 
 
 
 
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
 
Reply With Quote
 
 
 
 
RVic
Guest
Posts: n/a
 
      07-29-2009
Lew,

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



On Jul 29, 12:15*pm, Lew <l...@lewscanon.com> 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


 
Reply With Quote
 
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{
// append((char) payload) to what I am reading
}

Is that typically then how this can be done
 
Reply With Quote
 
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.
 
Reply With Quote
 
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
 
Reply With Quote
 
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.
 
Reply With Quote
 
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
 
Reply With Quote
 
Roedy Green
Guest
Posts: n/a
 
      07-29-2009
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.
 
Reply With Quote
 
Roedy Green
Guest
Posts: n/a
 
      07-29-2009
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.
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
What is the point of having 16 bit colour if a computer monitor can only display 8 bit colour? How do you edit 16 bit colour when you can only see 8 bit? Scotius Digital Photography 6 07-13-2010 03:33 AM
Parity Check Ed VHDL 18 10-06-2009 12:43 PM
stand-alone JMS, other JDBC operations, and transactions ( ActiveMQ + JOTM + JDBC operations ) Jesus M. Salvo Jr. Java 2 02-11-2006 06:33 PM
64 bit - Windows Liberty 64bit, Windows Limited Edition 64 Bit, Microsoft SQL Server 2000 Developer Edition 64 Bit, IBM DB2 64 bit - new ! vvcd Computer Support 0 09-17-2004 08:15 PM
64 bit - Windows Liberty 64bit, Windows Limited Edition 64 Bit,Microsoft SQL Server 2000 Developer Edition 64 Bit, IBM DB2 64 bit - new! Ionizer Computer Support 1 01-01-2004 07:27 PM



Advertisments
 



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57