forked from matyb/java-koans
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathAboutBitwiseOperators.java
More file actions
65 lines (55 loc) · 1.68 KB
/
Copy pathAboutBitwiseOperators.java
File metadata and controls
65 lines (55 loc) · 1.68 KB
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
package beginner;
import com.sandwich.koan.Koan;
import static com.sandwich.koan.constant.KoanConstants.__;
import static com.sandwich.util.Assert.assertEquals;
public class AboutBitwiseOperators {
@Koan
public void fullAnd() {
int i = 1;
if (true & (++i < 8)) i = i + 1;
assertEquals(i, __);
}
@Koan
public void shortCircuitAnd() {
int i = 1;
if (true && (i < -28)) i = i + 1;
assertEquals(i, __);
}
@Koan
public void aboutXOR() {
int i = 1;
int a = 6;
if ((a < 9) ^ false) i = i + 1;
assertEquals(i, __);
}
@Koan
public void dontMistakeEqualsForEqualsEquals() {
int i = 1;
boolean a = false;
if (a = true) i++;
assertEquals(a, __);
assertEquals(i, __);
// How could you write the condition 'with a twist' to avoid this trap?
}
@Koan
public void aboutBitShiftingRightShift() {
int rightShift = 8;
rightShift = rightShift >> 1;
assertEquals(rightShift, __);
}
@Koan
public void aboutBitShiftingLeftShift() {
int leftShift = 0x80000000; // Is this number positive or negative?
leftShift = leftShift << 1;
assertEquals(leftShift, __);
}
@Koan
public void aboutBitShiftingRightUnsigned() {
int rightShiftNegativeStaysNegative = 0x80000000;
rightShiftNegativeStaysNegative = rightShiftNegativeStaysNegative >> 4;
assertEquals(rightShiftNegativeStaysNegative, __);
int unsignedRightShift = 0x80000000; // always fills with 0
unsignedRightShift >>>= 4; // Just like +=
assertEquals(unsignedRightShift, __);
}
}