+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
\ No newline at end of file
diff --git a/src/main/java/LightSwitcher.java b/src/main/java/LightSwitcher.java
index 4499c39..0ca36cc 100644
--- a/src/main/java/LightSwitcher.java
+++ b/src/main/java/LightSwitcher.java
@@ -13,7 +13,7 @@ public class LightSwitcher {
* NOTE: If the initial state of a switch is already turned on, do not turn it off.
*/
public static int turnOnSwitches(int switches, int switchesToTurnOn) {
-
+ return switches | switchesToTurnOn;
}
/**
@@ -21,7 +21,7 @@ public static int turnOnSwitches(int switches, int switchesToTurnOn) {
* Remember to use bit notation (0bxxxxxxxx) and a bit operator.
*/
public static int turnOnAllSwitches(int switches) {
-
+ return switches | 0b11111111;
}
/**
@@ -30,16 +30,17 @@ public static int turnOnAllSwitches(int switches) {
* And a '1' in a position in 'switchesToTurnOff' means to turn that switch to off.
*/
public static int turnOffSwitches(int switches, int switchesToTurnOff) {
-
+ return switches & (~switchesToTurnOff);
}
/**
* Take a given input of switches and turn them all off.
* Remember to use bit notation and a bit operator.
*/
- public static int turnOffAllSwitches(int switches) {
-
- }
+
+ public static int turnOffAllSwitches(int switches) {
+ return switches & 0b00000000;
+ }
/**
* Take a given input of switches and the switches to flip, and return the new state of the switches.
@@ -47,7 +48,7 @@ public static int turnOffAllSwitches(int switches) {
* I.E switches = 1 0 1 and switchesToFlip = 1 1 0 should return 0 1 1.
*/
public static int flipSwitches(int switches, int switchesToFlip) {
-
+ return switches ^ switchesToFlip;
}
/**
@@ -55,7 +56,7 @@ public static int flipSwitches(int switches, int switchesToFlip) {
* NOTE: An integer has more than 8 bits, so find a way to only return the rightmost 8 bits.
*/
public static int flipAllSwitches(int switches) {
-
+ return (~switches & 0b11111111);
}
/**
@@ -64,14 +65,14 @@ public static int flipAllSwitches(int switches) {
* So, a byte reads 76543210
*/
public static int getSwitchPositionAt(int switches, int position) {
-
+ return ((switches >> position) & 00000001);
}
/**
* Move all the the bits to the right `count` places.
*/
public static int moveRightBy(int switches, int count) {
-
+ return switches >> count;
}
/**
@@ -79,7 +80,7 @@ public static int moveRightBy(int switches, int count) {
* NOTE: An integer has more than 8 bits, so find a way to only return the rightmost 8 bits.
*/
public static int moveLeftBy(int switches, int count){
-
+ return (switches << count) & 0b11111111;
}
/**
diff --git a/src/test/java/LightSwitcherTest.java b/src/test/java/LightSwitcherTest.java
index 071bfd5..d7a83d6 100644
--- a/src/test/java/LightSwitcherTest.java
+++ b/src/test/java/LightSwitcherTest.java
@@ -1,11 +1,18 @@
import org.junit.Assert;
+import org.junit.Before;
import org.junit.Test;
import static org.junit.Assert.*;
public class LightSwitcherTest {
- private int baseByte = 0b10101010;
- private int flippedBaseByte = 0b01010101;
+ private int baseByte;
+ private int flippedBaseByte;
+
+ @Before
+ public void setUp() {
+ baseByte = 0b10101010;
+ flippedBaseByte = 0b01010101;
+ }
@Test
public void testTurnOnSwitches() {
@@ -85,10 +92,11 @@ public void testFlipAllSwitches() {
int result2 = LightSwitcher.flipAllSwitches(flippedBaseByte);
int result3 = LightSwitcher.flipAllSwitches(0b11111111);
int result4 = LightSwitcher.flipAllSwitches(0b00000000);
- Assert.assertEquals(result1, 0b01010101);
- Assert.assertEquals(result2, 0b10101010);
- Assert.assertEquals(result3, 0b00000000);
- Assert.assertEquals(result4, 0b11111111);
+ System.out.println(0b01010101);
+ Assert.assertEquals(0b01010101, result1);
+ Assert.assertEquals(0b10101010, result2);
+ Assert.assertEquals(0b00000000, result3);
+ Assert.assertEquals(0b11111111, result4);
}
@Test