bits comparison

V

vib

Hi there,

Below is the code that compares bit by bit of 32-bits word. Two
errors(bits) are allowed. I am not sure if this is the right way to
check for x number of error bits. Advice and suggestion are all
welcome.

/* not compiler tested */

unsigned int BitMask;
unsigned int inWord;
unsigned int pattern;
unsigned int errCnt;
unsigned int temp;

Mask = 0x1;
errCnt = 0;
for ( i= 0; i< 32; i++)
{
temp = (inWord&BitMask ) & (pattern&BitMask ) )
if(!temp)
{
if(++errCnt > 2)
break;
}
BitMask = BitMask << 1;
}

Thanks in advance.

vib
 
V

vib

Oops! The corrected code

for ( i= 0; i< 32; i++)
{
if (inWord&BitMask ) != (pattern&BitMask ) )
{
if(++errCnt > 2)
break;
}
BitMask = BitMask << 1;
}

vib
 
P

pete

vib said:
Hi there,

Below is the code that compares bit by bit of 32-bits word. Two
errors(bits) are allowed. I am not sure if this is the right way to
check for x number of error bits. Advice and suggestion are all
welcome.

/* not compiler tested */

unsigned int inWord;
unsigned int pattern;
unsigned int errCnt;

errCnt = bit_count(inWord ^ pattern);

unsigned bit_count(unsigned n)
{
unsigned count;

for (count = 0; n != 0; n &= n - 1) {
++count;
}
return count;
}
 
V

vib

pete said:
errCnt = bit_count(inWord ^ pattern);

unsigned bit_count(unsigned n)
{
unsigned count;

for (count = 0; n != 0; n &= n - 1) {
++count;
}
return count;
}

Brilliant! the n &= n -1;

Many thanks.
vib
 
P

pete

vib said:
Brilliant! the n &= n -1;

I'm glad you like it. It's not original.

Also note that the solution works for any size unsigned int
with any number of bits.
 
J

Jason Curl

vib said:
Hi there,

Below is the code that compares bit by bit of 32-bits word. Two
errors(bits) are allowed. I am not sure if this is the right way to
check for x number of error bits. Advice and suggestion are all
welcome.

There is never a "right" way to do something and if it works, then it
could be considered "right". Some hints:
- C doesn't have bit-test operators, you will need to do everything with
bit manipulation operators like what you've done.
- The "XOR" operation could be used to check what bits are the same, and
what bits are different, e.g.

errCnt = 0;
temp = ~inWord ^ pattern;
while(temp && errCnt < 2) {
errCnt+=temp & 0x01;
temp >>= 1;
}

I haven't tested it, but it should give you a general idea. It's a
little faster, but not by much.


/* not compiler tested */

unsigned int BitMask;
unsigned int inWord;
unsigned int pattern;
unsigned int errCnt;
unsigned int temp;

Mask = 0x1;
errCnt = 0;
for ( i= 0; i< 32; i++)
{
temp = (inWord&BitMask ) & (pattern&BitMask ) )

Here you don't need &BitMask twice.
 
V

vib

Jason said:
There is never a "right" way to do something and if it works, then it
could be considered "right". Some hints:
- C doesn't have bit-test operators, you will need to do everything with
bit manipulation operators like what you've done.
- The "XOR" operation could be used to check what bits are the same, and
what bits are different, e.g.
Thanks for the valuable advice.

vib
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
474,166
Messages
2,570,902
Members
47,442
Latest member
KevinLocki

Latest Threads

Top