473,408 Members | 2,813 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,408 software developers and data experts.

bits comparison

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

Nov 15 '05 #1
8 1665
vib

Oops! The corrected code

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

vib

Nov 15 '05 #2
vib wrote:

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;
}

--
pete
Nov 15 '05 #3
vib


pete wrote:
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

Nov 15 '05 #4
vib wrote:

pete wrote:
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;


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.

--
pete
Nov 15 '05 #5
vib
Yup, I know. That is indeed efficient for my resource limited
situations.

Nov 15 '05 #6
vib wrote:
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.
if(!temp)
{
if(++errCnt > 2)
break;
}
BitMask = BitMask << 1;
}

Thanks in advance.

vib

Nov 15 '05 #7
Jason Curl wrote:

vib wrote:
Hi there,
errCnt = 0;
temp = ~inWord ^ pattern;
I don't think that the ~ belongs there.
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.
if(!temp)
{
if(++errCnt > 2)
break;
}
BitMask = BitMask << 1;
}


--
pete
Nov 15 '05 #8
vib


Jason Curl wrote:
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

Nov 15 '05 #9

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

40
by: aku | last post by:
I'm looking for the absolute fastest way to count the nr of bits that are set to "1" in a string. Presumably I then first need the fastest way to do this in a byte. I think this is it, but...
53
by: Zhiqiang Ye | last post by:
Hi, All I am reading FAQ of this group. I have a question about this: http://www.eskimo.com/~scs/C-faq/q7.31.html It says: " p = malloc(m * n); memset(p, 0, m * n); The zero fill is...
7
by: sathyashrayan | last post by:
Group, Following function will check weather a bit is set in the given variouble x. int bit_count(long x) { int n = 0; /* ** The loop will execute once for each bit of x set,
32
by: Mark | last post by:
lets say i have a char, and i want to check if the 3rd bit is 0 or 1... how might i do this? thanks!
64
by: yossi.kreinin | last post by:
Hi! There is a system where 0x0 is a valid address, but 0xffffffff isn't. How can null pointers be treated by a compiler (besides the typical "solution" of still using 0x0 for "null")? -...
11
by: Harsha | last post by:
Hi.... This may sound a bit novice ( because i am), could you please tell the procedure to compare bits of two different files stored on a secondary memory...... Thank You....
1
by: aarthi28 | last post by:
Hi, I have a text file "test.txt" that contains a series of 0's and 1's. For example, 0000001000010100011000 I need to read these values into an array so that i can perform a comparison with...
40
by: KG | last post by:
Could any one tell me how to reverse the bits in an interger?
29
by: Virtual_X | last post by:
As in IEEE754 double consist of sign bit 11 bits for exponent 52 bits for fraction i write this code to print double parts as it explained in ieee754 i want to know if the code contain any...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.