Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

can you give me funciton which compare two binary in c++

Status
Not open for further replies.

Guest_imported

New member
Jan 1, 1970
0
i need function whcih combine two binary number.please help me out
 
Check into how to use the Bitwise-AND Operator: &

The following is from the MSDN library regarding the Bitwise-AND Operator: &

The bitwise-AND operator (&) compares each bit of its first operand to the corresponding bit of its second operand. If both bits are 1, the corresponding result bit is set to 1. Otherwise, the corresponding result bit is set to 0.

Example

In the following example, the bitwise-AND operator (&) compares the bits of two integers, nNumA and nNumB:

// Example of the bitwise-AND operator
int nNumA=1, nNumB=3, nNumC; // 00000001, 00000011

nNumC = nNumA & nNumB; // nNumC is now 1
 
look & operator may tell u whther both are equal or not but it will not tell u which one is greater or smaller number . for that u have to add a bit of extra code .

if i get time i will give u the code afterwards u can mail me a reminder


 
I dont think the & operator WILL tell you if they are equal if used alone. WIth the example

3 & 1
or in binary 11 & 1 we find that it is true with the logical and

you can make life easy and use memcmp and that will return 0 if equal, <0 if buffer 1 less then buffer 2 and >0 if buffer 1 greater then buffer 2 and you can compare any amount of size with them as well.

If you just want to go binary i would say you want to do the following

x & y are 2 unknown values of the same size

if they are equal then

(x & ~y) would equal zero

there are other ways to go about this, this is just one :)


Matt
 
If you are dealing with 2 binary numbers and for whatever reason must deal with the numbers in binary form
then something like the following might help :

#define EQUAL 1
#define X_GREATER 2
#define Y_GREATER 3

VOID compare (WORD x, WORD y, INT &comparison)
// Compare two 16 bit binary numbers.
{ if (!x^y)
{//numbers are the same
comparison = EQUAL;
}
else
{ while ((x & 0x8000) && (y & 0x8000))
{ // Test MSB and shift if both set.
x << 1 , y << 1;
}
comparison = x & 0xF000 ? X_GREATER : Y_GREATER;
}
}

There are a hundred and one different ways to do this sort of thing ( and plenty I'm sure better than
the example above ) but hopefully this will give you some ideas.
 
Mr. Bodhi u have really done great . I must admit it , but there is a small problem.

if (!x^y)

The statment will always be true just check it . U have done a slight typing mistake but this mistake will not make ur program work properly . Instead give

if (! (x^y))

ne ways keep up the good work. Keep helping people like us who are always in need of some help from people like u
Santosh Thankachan

alternate email ids :
santoshthankachan@yahoo.com
 
oops - thanks santosh.

While we are on the subject I've just noticed that the while statement should be

while ((x & 0x8000) == (y & 0x8000) in case both bits are clear.

Two bugs in 10 lines - that's about right for me :)
 
hi bodhi , i have tried to modify ur code a bit . This may work properly. ne ways if u have ne thing related with heap please do give me and answer my query on my thread .

here is the modified code
VOID compare (WORD x, WORD y, INT &comparison)
// Compare two 16 bit binary numbers.
{
if (!(x^y))
{
//numbers are the same
comparison = EQUAL;
}
else
{
WORD temp = 0x8000 ;
while ((x & temp) == (y & temp))
{
// Test MSB and shift if both set.
x <<= 1 ;
y <<= 1;
temp = temp >> 1 ;
}
if ((temp & x))
comparison = X_GREATER ;
else
comparison = Y_GREATER;
}
}



Santosh Thankachan
Email id :santoshthankachan@yahoo.com
 
sorry the last code is not behaving properly
i didn't tested it properly .

When i get time i will try to look into it . please excuse me
Santosh Thankachan
Email id :santoshthankachan@yahoo.com
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top