본문 바로가기
자바 프로그래밍/코딩

병렬 패리티 비트

by 세인트킴 2023. 4. 26.
public class ex07_1 {

public static void main(String[] args) {
int[][] bit = {{1, 0, 1, 0, 1, 1, 1, 1},
{1, 0, 0, 0, 0, 0, 1, 1},
{0, 1, 0, 0, 0, 0, 0, 0},
{1, 1, 1, 1, 0, 0, 0, 0},
{1, 0, 1, 1, 1, 0, 0, 1},
{0, 0, 0, 0, 0, 1, 1, 1},
{1, 1, 1, 1, 1, 1, 1, 1},
{0, 1, 1, 1, 1, 0, 0, 0}};

for(int width=0; width<bit.length; width++) {
for(int height=0; height<bit[width].length; height++) {
System.out.printf("%d ", bit[width][height]);
}
int rowParity = widthParity(bit[width]);
System.out.printf("%d ",rowParity);
System.out.println();
}

for(int height=0; height<bit[0].length; height++) {
int colParity = heightParity(bit, height);
System.out.printf("%d ", colParity);
}

int totalParity = 0;
for(int width=0; width<bit.length; width++) {
totalParity += widthParity(bit[width]);
}
for(int height=0; height<bit[0].length; height++) {
totalParity += heightParity(bit, height);
}
System.out.printf("%d\n", totalParity%2);
}
public static int widthParity(int[] arr) {
int count = 0;
for(int width : arr) {
if(width == 1) {
count++;
}
}
return count%2;
}
public static int heightParity(int[][] arr, int height) {
int count = 0;
for(int width=0; width<arr.length; width++) {
if(arr[width][height] == 1) {
count++;
}
}
return count%2;
}
}

'자바 프로그래밍 > 코딩' 카테고리의 다른 글

ex06_3.java  (0) 2023.05.06
ex06_2.java  (0) 2023.05.06
ex06_1.java  (0) 2023.05.06
짝수 패리티 비트  (0) 2023.04.26
TestAverage  (0) 2023.04.24