Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.util.Scanner;
- public class _5_FriendBits {
- public static void main(String[] args) {
- // TODO Auto-generated method stub
- Scanner scan = new Scanner(System.in);
- long number = scan.nextLong();
- long friendBits = 0;
- long aloneBits = 0;
- if (number == 0 || number == 1) {
- aloneBits = number;
- } else {
- int startBit = getMostLeftBitPositionInTheNumber(number);
- long bitValue, previousBit, nextBit;
- for (int bitPos = startBit; bitPos >= 0; bitPos--) {
- previousBit = (number >>> (bitPos + 1)) & 1;
- bitValue = (number >>> bitPos) & 1;
- nextBit = (number >>> (bitPos - 1)) & 1;
- if ((bitPos < startBit && bitValue == previousBit)
- || (bitPos > 0 && bitValue == nextBit)) {
- friendBits <<= 1;
- friendBits |= bitValue;
- } else {
- aloneBits <<= 1;
- aloneBits |= bitValue;
- }
- }
- }
- System.out.println(friendBits);
- System.out.println(aloneBits);
- }
- private static int getMostLeftBitPositionInTheNumber(long number) {
- // TODO Auto-generated method stub
- for (int i = 31; i > 0; i--) {
- long bitValue = ((1 << i) & number);
- if (bitValue > 0) {
- return i;
- }
- }
- return 0;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement