Advertisement
n_stefanov

Ex8_CountOfEqualBitPairs

May 11th, 2014
393
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.29 KB | None | 0 0
  1. package javaSyntax;
  2.  
  3. import java.util.Scanner;
  4.  
  5. public class Ex8_CountOfEqualBitPairs {
  6.  
  7.     public static void main(String[] args) {
  8.         Scanner in = new Scanner(System.in);
  9.         int n = in.nextInt();
  10.         int bit = 0;
  11.         int counter = 0;
  12.         int foundPairs = 0;
  13.  
  14.         while (n > 0)
  15.         {
  16.            
  17.             if ((n & 1) == 1 && bit == 0)
  18.             {
  19.                 counter = 0;
  20.                 bit = 1;
  21.             }
  22.  
  23.             if ((n & 1) == 0 && bit == 1)
  24.             {
  25.                 counter = 0;
  26.                 bit = 0;
  27.             }
  28.  
  29.             if ((n & 1) == 1)
  30.             {
  31.                 bit = 1;
  32.             }
  33.             else
  34.             {
  35.                 bit = 0;
  36.             }
  37.  
  38.             if ((n & 1) == 1 && bit == 1)
  39.             {
  40.                 counter++;
  41.                 if (counter == 2)
  42.                 {
  43.                     foundPairs++;
  44.                     counter = 1;
  45.                 }
  46.             }
  47.  
  48.             if ((n & 1) == 0 && bit == 0)
  49.             {
  50.                 counter++;
  51.                 if (counter == 2)
  52.                 {
  53.                     foundPairs++;
  54.                     counter = 1;
  55.                 }
  56.             }
  57.  
  58.             n = n >> 1;
  59.         }
  60.         System.out.println(foundPairs);
  61.     }
  62. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement