0

I have the following Java code:

long a = Long.parseLong("11001100", 2);
long b = Long.parseLong("11000000", 2);
int npos = 0 ;
int pos = 0 ;
long n = ~(a ^ b) ;
int cnt = 0;
while (n != 0) {
  pos++ ;
  if ((n & 3) == 3) cnt++; // count how many matches
  else{
    npos = pos ;  // position that not matched also giving wrong which should be 2 nd position.
  }
  n >>>= 2;
}

System.out.println(npos + "  " + cnt) ; // also print which two bits are not matched i.e. 00 and 11

I am trying to find how many two-bit sequences match in two integers. I want also to find which two bits are not matched. Can anybody help me how to do that?

PS: I don't have the string in my original code, only have integers. Therefore, I can't do string manipulation.

Edit:

long a = Long.parseLong("11000100", 2);
long b = Long.parseLong("11000000", 2);
long mask = 0x03;
int npos = 0 ;
int cnt = 0;
long p1 = 0;
long p2 = 0;
for (int pos = 0; pos < 64; pos++, mask <<= 2) {

  if ((a & mask) == (b & mask)) {
     cnt++; // count how many matches
  } else {
    npos = pos ;  // *last* position that did not match
    p1 = (a & mask) ; // two bits that not matched
    p2 = (b & mask) ; // two bits that not matched
  }
}

  System.out.println(npos + "  " + cnt + " " + p1 + " " + p2) ; // also print which two bits are not matched i.e. 00 and 01
Arpssss
  • 3,850
  • 6
  • 36
  • 80
  • 1
    Regarding your closing comment - if string manipulation were the best way to solve this, it is *trivial* to get a String from the ints passed in (`String.valueOf(a)`). – Andrzej Doyle Aug 07 '12 at 18:49
  • @AndrzejDoyle fwiw, there's also `Integer.toString(a)`. – Dennis Meng Aug 07 '12 at 18:51
  • I have no idea what this question is asking for. – Wug Aug 07 '12 at 18:52
  • Just to get an idea. What is the expected result for your sample a = 11001100 and b = 11000000 ? – eburgos Aug 07 '12 at 18:53
  • @AndrzejDoyle, string operation will be expensive as I have to do lots of this kind of operations. There is no faster method to do that ? – Arpssss Aug 07 '12 at 18:54
  • @Wug OP wants to take two ints, convert them into binary, compare them two bits at a time to see if they share the same bits, and print out info about it. – Dennis Meng Aug 07 '12 at 18:56
  • And also "to find which two bits are not matched" which is meaningless to me. can there be more than one nonmatching area? – Wug Aug 07 '12 at 19:00
  • @eburgos, 00, 11 not matched. and position in 2, number of matches 31. – Arpssss Aug 07 '12 at 19:01
  • @wug, may be but for this moment I give it to make simple one. – Arpssss Aug 07 '12 at 19:08
  • Are your 2-bit sequences strictly separated, or would 111 be two sequences (first from bits 1 and 2, second from 2 and 3). That is would two binary numbers 1111 and 1111 produce just 2 matches (separated sequences), or would they produce 3 matches (the second case - sequence can begin anywhere)? – Humungus Aug 07 '12 at 19:09
  • @Humungus, will give 2 matches. – Arpssss Aug 07 '12 at 19:10
  • n = a&b instead of n = ~(a^b), right? Or am I totally missundertsanding you here? And question 2, when you say "two integers" do you mean two numbers belonging to N or, java ints or something else? – esej Aug 07 '12 at 19:20
  • @esej, XNOR to find bit mismatches. say 1100 XNOR 0000. And numbers are java int/long. – Arpssss Aug 07 '12 at 19:28
  • @Arpssss I hadn't given any thought to whether string manipulation was actually faster or not. I was just referring to your closing sentence, which implied that *you thought* string manipulation would be a good yet impossible way to do this. I just wanted to point out that **if** it turns out to be good, you can very easily derive the strings for your inputs. – Andrzej Doyle Aug 08 '12 at 15:44

1 Answers1

3

You parse the integers as base-10 numbers, where you probably wanted to parse them as binary integers, to do that use the method that has a radix parameter:

long a = Long.parseInt("11001100", 2);
long b = Long.parseInt("11000000", 2);

It might be easier to just run a loop comparing the 2 values using a mask:

long mask = 0x03;
int npos = 0 ;
int cnt = 0;

for (int pos = 0; pos < 32; pos++, mask <<= 2) {

  if ((a & mask) == (b & mask)) {
     cnt++; // count how many matches
  } else {
    npos = pos ;  // *last* position that did not match
  }
}
rsp
  • 23,135
  • 6
  • 55
  • 69
  • sorry, that should be. But, how to find position and mismatched value ? – Arpssss Aug 07 '12 at 19:03
  • if you need to handle the mismatches, you could add their position and mask to a `List` to inspect after the loop. – rsp Aug 07 '12 at 19:16
  • As rsp said, a List can come in handy. Also I'd rather see the loop condition as pos < Integer.SIZE (but yeah, 32 should do). And increment pos by 2, not 1, since we're moving two spaces. – Humungus Aug 07 '12 at 19:21
  • Actually, my code is wrong in that sense that in my example it should give position 2 not matched, but it gives position 5. – Arpssss Aug 07 '12 at 19:21
  • So, as your code. And I want the two bits also that mismatched. – Arpssss Aug 07 '12 at 19:24
  • The mismatch is given by `(mask, a & mask`, b & mask)` how to keep that information depends on what you intend to use it for later. – rsp Aug 07 '12 at 19:27