Created
May 24, 2017 14:34
-
-
Save fermopili/666cae3e93d6936de1b05598c719a7e5 to your computer and use it in GitHub Desktop.
com.javarush.task.task39.task3903
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.javarush.task.task39.task3903; | |
import java.io.BufferedReader; | |
import java.io.IOException; | |
import java.io.InputStreamReader; | |
/* | |
Неравноценный обмен | |
*/ | |
public class Solution | |
{ | |
public static void main(String[] args) throws IOException | |
{ | |
BufferedReader reader = new BufferedReader ( new InputStreamReader ( System.in ) ); | |
System.out.println ( "Please type in a number: " ); | |
long number = Long.parseLong ( reader.readLine ( ) ); | |
System.out.println ( "Please type in first index: " ); | |
int i = Integer.parseInt ( reader.readLine ( ) ); | |
System.out.println ( "Please type in second index: " ); | |
int j = Integer.parseInt ( reader.readLine ( ) ); | |
System.out.println ( "The result of swapping bits is " + swapBits ( number, i, j ) ); | |
} | |
public static long swapBits(long number, int i, int j) | |
{ | |
long bitI = (number>>i)&0x01; | |
long bitJ = (number>>j)&0x01; | |
if (bitI != bitJ) | |
number = number & ~((1 << i) | (1 << j)) | (bitI << j) | (bitJ << i); | |
return number; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment