-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathNibble_swap.java
More file actions
51 lines (32 loc) · 946 Bytes
/
Nibble_swap.java
File metadata and controls
51 lines (32 loc) · 946 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
import java.util.*;
public class Nibble_swap
{
public static void main(String[] args) {
Scanner scan = new Scanner(System.in);
// If integer is given
/*
int givenNumber = scan.nextInt();
String givenByte = Integer.toBinaryString(givenNumber);
if(givenByte.length() < 8)
{
int l = 8 - givenByte.length();
while(l>0)
{
givenByte = "0" + givenByte;
l--;
}
}
*/
String givenByte = scan.next();
if(givenByte.length() != 8)
{
System.out.println("Invalid input.");
}
else {
String s1 = givenByte.substring(0,4);
String s2 = givenByte.substring(4,8);
String swapped = s2 + s1;
System.out.println(Integer.parseInt(swapped,2));
}
}
}