题目
对于DES的S1,计算下列随机变量的偏差
X
2
⨁
Y
1
⨁
Y
2
⨁
Y
3
⨁
Y
4
{X_2}\bigoplus{Y_1}\bigoplus{Y_2}\bigoplus{Y_3}\bigoplus{Y_4}
X2⨁Y1⨁Y2⨁Y3⨁Y4
实现
#include<bits/stdc++.h>
using namespace std;
int main()
{
int a[4][16] = {{14,4,13,1,2,15,11,8,3,10,6,12,5,9,0,7},
{0,15,7,4,14,2,13,1,10,6,12,11,9,5,3,8},
{4,1,14,8,13,6,2,11,15,12,9,7,3,10,5,0},
{15,12,8,2,4,9,1,7,5,11,3,14,10,0,6,13}};
int count = 0;
for(int i=0;i<64;i++)
{
unsigned short b1 = (i>>5) *2;
unsigned short b6 = i&1;
unsigned short b1b6 = b1+b6;
unsigned short b25 = (i&30)/2;
unsigned short y = a[b1b6][b25];
if(((i&16)/16 +(y&1)+(y&2)/2+(y&4)/4+(y&8)/8)%2 == 0)
count++;
}
printf("%d",count);
return 0;
}
count/64减去1/2即为偏差的值
欢迎指正