|
-
April 21st, 2002, 01:57 PM
#3
Junior Member
Hi,Greg,
Sorry for confusion.Since it is part of a program. However,I could explain in the following way:
I got a byte array: byte cryptogram[8];
The value is given: cryptogram[0]= 00011010;(Actually I couldn't find syntax like this in Java,
just assume it is a byte with value "00011010").
cryptogram[1]=00110110;
.........
cryptogram[7]=00110101;
Now what I was asked to do is count how many '1' in this byte array.
So : cryptogram[0] has three '1', cryptogram[1] has four '1',etc,then add
3+4+....+4;
This is part of program to study diffusion property of an encryption algorithm called OMEA.
The only way I could think about is:
int a,count=0;
for(int i=0;i<8;i++)
{
a=(int)cryptogram[i];
while( a!=0)
{
if(a%2==1)
count++;
a=a/2;
}
}
I found it is a bit messy,I was thinking to use one of bit operation,to get out number of 1 's in one go.Could you help me with that ?
Thanks and regards.
nancen
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
|