السلام عليكم انا طالبة مبتائة في الجافا اتمنى تشوفوا شو الخطا في حل
هالاسئلة
1 Problem Specification
Find a suitable algorithm to calculate the parity bit for a given sequence of bits (0's and 1's). We have
two types of parity: even parity and odd parity as explained below.
1.1 Even Parity
Given a sequence of bits you get even parity by adding an additional bit number 1 or 0 such that the
total number of 1's is even as follows.
1001001 -> parity bit = 1
0010100 -> parity bit = 0
1.2 Odd Parity
Given a sequence of bits you get odd parity by adding an additional bit number 1 or 0 such that the
total number of 1's is odd as follows.
1001001 -> parity bit = 0
0010100 -> parity bit = 1
هذا الحل
import java.io.UnsupportedEncodingException;
import static java.lang.System.out;
public final class TestParity
{
// -------------------------- STATIC METHODS --------------------------
private static boolean isOddParity( final byte b )
{
int bb = b;
int bitCount = 0;
for ( int i = 0; i < 8; i++, bb >>>= 1 )
{
if ( ( bb & 1 ) != 0 )
{
bitCount++;
}
}
return ( bitCount & 1 ) != 0;
}
private static boolean isOddParityViaSosman( final byte b )
{
final int bb = b & 0xff;
int parity = bb ^ ( bb >> 4 );
parity ^= parity >> 2;
parity ^= parity >> 1;
return ( parity & 1 ) != 0;
}
public static void main( String[] args ) throws UnsupportedEncodingException
{
out.println( isOddParity( ( byte ) 0xff ) ); // false
out.println( isOddParity( ( byte ) 0x70 ) ); // true
out.println( isOddParityViaSosman( ( byte ) 0xff ) ); // false
out.println( isOddParityViaSosman( ( byte ) 0x70 ) ); // true
}
}

