Re: bit operations and parity



Mayeul <mayeul.marguet@xxxxxxx> wrote in
news:4a708393$0$9973$426a74cc@xxxxxxxxxxxx:

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

.



Relevant Pages

  • Re: bit operations and parity
    ... Only one shift is needed per iteration, so there are no questions about whether the processor has a barrel shifter. ... exercise for the reader. ... (Extending it to float and double is left ...
    (comp.lang.java.programmer)
  • Re: circular shifting a vector/matrix - Problem
    ... corrCoef = zeros(1, numColMatrix01); ... for shiftSize = 1:numColMatrix01, ... Are you sure you want to shift like this (iteration 1, 1 shift, ...
    (comp.soft-sys.matlab)
  • security considerations for: set x dir/[*] dir/*
    ... The following steps for the iteration are: ... contents of $is under the control of an attacker (and this shell ...
    (comp.unix.shell)
  • Re: Position of title changes in each loop. Why?
    ... So I expect titleposition to be the ... You are shifting the title slightly up in each iteration. ... I assume you are plotting on the same axis in each loop. ... You only need to shift it once. ...
    (comp.soft-sys.matlab)
  • Re: convergence of QR-algorithm
    ... >Bill, yes I'm doing all of that., alongside using a shift that is the ... In either case the bottom two elements below the diagonal should ... >the one I'm using as it involves more calculation at each iteration. ... >case convergence could never occur. ...
    (sci.math.num-analysis)