GIDForums  

Go Back   GIDForums > Computer Programming Forums > C Programming Language
User Name
Password
Register FAQ Members List Calendar Search Today's Posts Mark Forums Read

 
 
Thread Tools Search this Thread Rate Thread
  #1  
Old 09-Nov-2007, 22:59
CeZ CeZ is offline
New Member
 
Join Date: Nov 2007
Posts: 2
CeZ is on a distinguished road

C code, matching binary pairs of numbers


Hi!

I'm having a little trouble with a problem I hope someone can please help with! I'm looking at binary numbers that only differ by one particular pair of elements... (it has to be exactly one, they can't be the same or differ by two etc)...

So a and b in this example:
a = [21] - [01(01)01]
b = [25] - [01(10)01]
differ only by their middle pair... and so because it's only a difference of 1 pair, the value here would be 0.5...

Whereas a and b in this example:
a = [37] - (10)(01)01
b = [25] - (01)(10)01

differ by 2 pairs, so the value here would be 0...

Only numbers that have a single pair difference are given the value 0.5...

Given 2 decimal numbers I'd like to be to pass them to a function which will convert them to binary numbers of length N (variable dependent on array size), and check for the pair situation described above and then return the appropriate value...

This is what I have so far:

CPP / C++ / C Code:
double F(int a, int b)
{
     int c, d = a ^ b;

     for (c = 0; d; c++)
          d &= d - 1;

     if ((c / 2) > 1)
          return 0.0;
     
     return 0.5;
}
  #2  
Old 10-Nov-2007, 08:32
davekw7x davekw7x is offline
Outstanding Member
 
Join Date: Feb 2004
Location: Left Coast, USA
Posts: 4,701
davekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to behold

Re: C code, matching binary pairs of numbers


Quote:
Originally Posted by CeZ
which will convert them to binary numbers of length N (variable dependent on array size),

Huh???

What does this mean? How are you representing the numbers? What does "array size" have to do with anything?

If the values of the variables are represented internally as 32-bit binary numbers, then you could do something like the following:

Code:
You have an integer variable, c. This will hold a count of the number of places where bit pairs are different. Declare an unsigned integer named mask. Set d equal to a exclusive-ored with b. (You do this already.) The result is a number that has 1's in bit positions where a and b are different, and 0's in bit positions where a and b are the same. Make a loop that starts with c = 0 (the count) and mask = 0xc0000000. (Upper two bits equal to 1) The loop will terminate when mask is equal to zero. After executing the statements in the loop, shift the mask right by two bits. Statement(s) inside the loop will increment the counter whenever (the mask anded with d) is not equal to zero. When the loop terminates, check the value of c to determine the return value of the function.

If this doesn't satisfy your requirements, then maybe you could give us a better statement of the problem.


Regards,

Dave
Last edited by davekw7x : 10-Nov-2007 at 09:06.
  #3  
Old 13-Nov-2007, 13:00
MonkOfox MonkOfox is offline
New Member
 
Join Date: Nov 2007
Posts: 21
MonkOfox is on a distinguished road

Re: C code, matching binary pairs of numbers


to convert from decimal to binary is easy.

you just have a loop that says the following..
CPP / C++ / C Code:

int [] returnBinary(int mynum){
current_bit = 128;
int array[8];
int i = 0;
while(not at end of array)
{
     if(mynum >= current_bit){
     //set the  spot in the array at i  to 1
     current_bit = current_bit/2;
     mynum = mynum - current_bit;}
     else{
     // set the spot in this array at i to 0
     }
i++;
}

return array;

}
hope this helps you with the binaray, then you can just compare the numbers to each
other and group every other two.

this code is also assuming you only have 8-bit binary numbers at the most (255 being the highest decimal value you can use).

MonkO
  #4  
Old 13-Nov-2007, 13:16
fakepoo fakepoo is offline
Regular Member
 
Join Date: Oct 2007
Posts: 482
fakepoo is a jewel in the roughfakepoo is a jewel in the roughfakepoo is a jewel in the rough

Re: C code, matching binary pairs of numbers


Another way would be to use a union with bit allocations.
CPP / C++ / C Code:
union MyByte
{ char TheByte;
  struct TheBits
  { bit0 : 1;
    bit1 : 1;
    bit2 : 1;
    bit3 : 1;
    bit4 : 1;
    bit5 : 1;
    bit6 : 1;
    bit7 : 1;
  }
};
This way, you could set the Byte and read the individual bits because they share the same memory. (The bit order might be reversed, I can't remember off hand)
  #5  
Old 13-Nov-2007, 19:25
CeZ CeZ is offline
New Member
 
Join Date: Nov 2007
Posts: 2
CeZ is on a distinguished road

Re: C code, matching binary pairs of numbers


Thanks very much for all the help... I finally figured it out:

CPP / C++ / C Code:
float F(int a,int b,int N)
{
//xor the two numbers
int temp=a^b;

//if the temp equals 0, all bits are 0, no diff pair found
if(temp==0)
return 0;

int test=temp%2;
int counter=0;

//remove all left 0 until 1 found
while(test!=1)
{
temp=temp>>1;
test=temp%2;
counter++;
}

//if the value is equal to 3, one pair exists
if(temp==3)
{
if(fetch(a,counter+1)==fetch(b,cou  nter))
return 0.5;
else
{
return 0.0;
}
}else
{
return 0.0;
}
}

unsigned short fetch(unsigned short i,unsigned short j)
{
unsigned short temp = i;
unsigned short t = 0;

while(temp>0)
{
if(j==t)
return temp % 2;
t++;
temp/=2;
}

return 0;

}
 
 

Recent GIDBlogToyota - 2008 September Promotion by Nihal

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
New to programming (Homework Help) fishguts Miscellaneous Programming Forum 1 12-Sep-2007 10:50
How to sort random access file? wmmccoy0910 C Programming Language 12 04-Sep-2006 03:40
how to make a 'INTEGER to BINARY' code? justaHSstudent C++ Forum 1 02-Oct-2005 14:22
Linear Search eccoflame C Programming Language 3 19-Apr-2005 08:36
Problem with int mixed with char,... leitz C++ Forum 17 07-Dec-2004 20:56

Network Sites: GIDNetwork · GIDWebHosts · GIDSearch · Learning Journal by J de Silva, The

All times are GMT -6. The time now is 15:15.


vBulletin, Copyright © 2000 - 2008, Jelsoft Enterprises Ltd.