473,395 Members | 1,411 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,395 software developers and data experts.

Compare between byte

Is there any way to compare two byte by XOR in a fast way
and return the number of bits is equal .

e.g.
b1 = 11100011
b2 = 01010010

after XOR compare will return 4 bit is equal
--
http://bbs.find-tutors.com/viewforum.php?f=30
Nov 15 '05 #1
4 2931
Sorry , It should be AND operation

--
BT.NET ¤U¤@¥N¬J BT client
http://bbs.find-tutors.com/viewforum.php?f=30

"chris chan" <50******@student.cityu.edu.hk> ¼¶¼g©ó¶l¥ó·s»D
:eg*************@TK2MSFTNGP10.phx.gbl...
Is there any way to compare two byte by XOR in a fast way
and return the number of bits is equal .

e.g.
b1 = 11100011
b2 = 01010010

after XOR compare will return 4 bit is equal
--
http://bbs.find-tutors.com/viewforum.php?f=30

Nov 15 '05 #2
Sorry,

for ( byte i = 0; i < 7; ++i )
should be
for ( byte i = 0; i < 8; ++i )

Regards

Chris Taylor

"Chris Taylor" <ch*************@hotmail.com> wrote in message
news:es**************@TK2MSFTNGP12.phx.gbl...
Hi,

I think the following function should help you achieve what you want. It
will count the number of bits that any integer, type up to a int for this
example, that they have in common.

byte countCommonBits( byte b1, byte b2 )
{
byte nCount = 0;

for ( byte i = 0; i < 7; ++i )
{
bool r1 = ( b1 & 1 ) == 1;
bool r2 = ( b2 & 1 ) == 1;
if ( r1 == r2 )
++nCount;
b1 >>= 1;
b2 >>= 1;
}

return nCount;
}
It works by check bit 0 of both byte, if they are the same then nCount is
incremented. Then the bit 1 is shifted into position 0 and the test is
repeated. This is done for all 8 bits of the byte datatype.

Hope this helps

Chris Taylor
"chris chan" <50******@student.cityu.edu.hk> wrote in message
news:eg*************@TK2MSFTNGP10.phx.gbl...
Is there any way to compare two byte by XOR in a fast way
and return the number of bits is equal .

e.g.
b1 = 11100011
b2 = 01010010

after XOR compare will return 4 bit is equal
--
http://bbs.find-tutors.com/viewforum.php?f=30


Nov 15 '05 #3
thx for your reply
i will have a try ^_^

--
"Chris Taylor" <ch*************@hotmail.com> ¼¶¼g©ó¶l¥ó·s»D
:Om**************@tk2msftngp13.phx.gbl...
Sorry,

for ( byte i = 0; i < 7; ++i )
should be
for ( byte i = 0; i < 8; ++i )

Regards

Chris Taylor

"Chris Taylor" <ch*************@hotmail.com> wrote in message
news:es**************@TK2MSFTNGP12.phx.gbl...
Hi,

I think the following function should help you achieve what you want. It
will count the number of bits that any integer, type up to a int for this example, that they have in common.

byte countCommonBits( byte b1, byte b2 )
{
byte nCount = 0;

for ( byte i = 0; i < 7; ++i )
{
bool r1 = ( b1 & 1 ) == 1;
bool r2 = ( b2 & 1 ) == 1;
if ( r1 == r2 )
++nCount;
b1 >>= 1;
b2 >>= 1;
}

return nCount;
}
It works by check bit 0 of both byte, if they are the same then nCount is incremented. Then the bit 1 is shifted into position 0 and the test is
repeated. This is done for all 8 bits of the byte datatype.

Hope this helps

Chris Taylor
"chris chan" <50******@student.cityu.edu.hk> wrote in message
news:eg*************@TK2MSFTNGP10.phx.gbl...
Is there any way to compare two byte by XOR in a fast way
and return the number of bits is equal .

e.g.
b1 = 11100011
b2 = 01010010

after XOR compare will return 4 bit is equal
--
http://bbs.find-tutors.com/viewforum.php?f=30



Nov 15 '05 #4

If performance is important, it would probably be faster to us a
static lookup table instead of calculating the number of bits on each
call.

static int[] OnesInByte = new int[] {
0, 1, 1, 2, 1, 2, 2, 3, 1, 2, 2, 3, 2, 3, 3, 4,
1, 2, 2, 3, 2, 3, 3, 4, 2, 3, 3, 4, 3, 4, 4, 5,
1, 2, 2, 3, 2, 3, 3, 4, 2, 3, 3, 4, 3, 4, 4, 5,
2, 3, 3, 4, 3, 4, 4, 5, 3, 4, 4, 5, 4, 5, 5, 6,
1, 2, 2, 3, 2, 3, 3, 4, 2, 3, 3, 4, 3, 4, 4, 5,
2, 3, 3, 4, 3, 4, 4, 5, 3, 4, 4, 5, 4, 5, 5, 6,
2, 3, 3, 4, 3, 4, 4, 5, 3, 4, 4, 5, 4, 5, 5, 6,
3, 4, 4, 5, 4, 5, 5, 6, 4, 5, 5, 6, 5, 6, 6, 7,
1, 2, 2, 3, 2, 3, 3, 4, 2, 3, 3, 4, 3, 4, 4, 5,
2, 3, 3, 4, 3, 4, 4, 5, 3, 4, 4, 5, 4, 5, 5, 6,
2, 3, 3, 4, 3, 4, 4, 5, 3, 4, 4, 5, 4, 5, 5, 6,
3, 4, 4, 5, 4, 5, 5, 6, 4, 5, 5, 6, 5, 6, 6, 7,
2, 3, 3, 4, 3, 4, 4, 5, 3, 4, 4, 5, 4, 5, 5, 6,
3, 4, 4, 5, 4, 5, 5, 6, 4, 5, 5, 6, 5, 6, 6, 7,
3, 4, 4, 5, 4, 5, 5, 6, 4, 5, 5, 6, 5, 6, 6, 7,
4, 5, 5, 6, 5, 6, 6, 7, 5, 6, 6, 7, 6, 7, 7, 8,
};
int countCommonBits( byte b1, byte b2 )
{
return OnesInByte[b1 & b2];
}

Mattias

--
Mattias Sjögren [MVP] mattias @ mvps.org
http://www.msjogren.net/dotnet/
Please reply only to the newsgroup.
Nov 15 '05 #5

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

Similar topics

19
by: David zhu | last post by:
I've got different result when comparing two strings using "==" and string.Compare(). The two strings seems to have same value "1202002" in the quick watch, and both have the same length 7 which I...
5
by: Jim H | last post by:
I will have many 3 byte Byte arrays. Is there a way to compare the values without iterating through and comparing each element in the code? Example: byte lTest1 = new byte {0x1, 0x2, 0x3};...
17
by: bengamin | last post by:
Hi, I have a C# class and two instance of the class; the class have some property. I want to compare the property value of the two instance How should i do? override == ? use delegate ?
1
by: todorov-fkt | last post by:
Hello everyone, I have a field which is 1 byte long and is used to store different flags (according to specs) - it is the flags bit in the ID3 tag header. So 0xabc00000 represents the byte,...
3
by: NathanV | last post by:
I'm trying to compare two byte's. I think I have to use binary operators. How can I tell if two bytes have the same value? Also, I have a function that is returning a byte value. Is there a...
5
by: sjoshi | last post by:
I have an SQLBinary (8) field(mts) with data like 0x01F4000000000000, 0x0400000000000000, etc. Now I need to compare just the first 4 bytes to an Int32 field (cts) and update the SQLBinary field if...
5
by: rcolby | last post by:
Evening, Wondering if someone can point me in the right direction, on how I would compare a system.guid with a system.byte. system.guid (pulled from sql server table with a data type of...
2
by: Tom | last post by:
What's the best way to compare two byte arrays? Right now I am converting them to base64 strings and comparing those, as so: 'result1 and result2 are two existing byte arrays that have been...
5
by: Oleg Subachev | last post by:
Is there other way of comparing two byte arrays than iterating through the two and compare individual bytes ? Oleg Subachev
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
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: 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
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
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.