-
Notifications
You must be signed in to change notification settings - Fork 1
/
Task05.java
108 lines (82 loc) · 2.81 KB
/
Task05.java
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
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
import java.util.Scanner;
public class Task05 {
public static void main(String[] args){
Scanner scan = new Scanner(System.in);
// Problem 1
System.out.println("Enter a, b, c and d values between [-5, 5] : ");
System.out.println("We assume that a < b and c < d !");
int a = scan.nextInt();
int b = scan.nextInt();
int c = scan.nextInt();
int d = scan.nextInt();
int max = 5;
int min = -5;
boolean state1;
boolean state2;
boolean state3;
boolean state4;
boolean state5;
for(int i = min ; i <= max; i++){
state1 = (i >= a) && (i <= b);
state2 = (i >= c) && (i <= d);
state3 = (state1 || state2);
state4 = (state1 && state2);
state5 = (state3 ^ state4);
System.out.println(i + ": inSum? " + state3 +
"; inIntersect? " + state4 + "; inSymDiff? " + state5 );
}
// Problem 2
System.out.println("Enter an int");
int n = scan.nextInt();
boolean is7thBitSet = false;
is7thBitSet = (n & 128) == 128;
System.out.println("Is 7th bit set? " + is7thBitSet);
int numberOf1s = 0;
int remainder = 0;
long total = 0;
int exponent = 0;
int mostSignificant = -1;
int firstNumber = n;
int swapped = 0;
int leastByte1 = 0;
int leastByte2 = 0;
for (int i = 0, j = 31; i < 32; i++, j--) {
if (((n >>> i) & 1) == 1) {
numberOf1s++;
}
}
System.out.println("No. of 1s is " + numberOf1s);
while (n > 0) {
remainder = n % 2;
total += remainder * Math.pow(10, exponent);
n /= 2;
exponent++;
}
if (n < 0) {
for (int i = 0, j = 31; i < 32; i++, j--) {
if (((n >>> j) & 1) == 1) {
total += 1 * Math.pow(10, exponent);
}
exponent++;
}
}
if (firstNumber != 0) {
mostSignificant = exponent - 1;
}
System.out.println("Most significant bit set: " + mostSignificant);
if (firstNumber < 0) {
leastByte1 = firstNumber << 24;
leastByte1 = leastByte1 >> 16;
leastByte2 = firstNumber << 16;
leastByte2 = leastByte2 >> 24;
swapped = (leastByte1 | leastByte2);
} else {
leastByte1 = firstNumber << 24;
leastByte1 = leastByte1 >>> 16;
leastByte2 = firstNumber << 16;
leastByte2 = leastByte2 >>> 24;
swapped = (leastByte1 | leastByte2);
}
System.out.println("With 2 least significant bytes" + " swapped: " + swapped);
}
}