diff --git a/src/main/java/LightSwitcher.java b/src/main/java/LightSwitcher.java index 4499c39..35eae58 100644 --- a/src/main/java/LightSwitcher.java +++ b/src/main/java/LightSwitcher.java @@ -1,19 +1,19 @@ -/** - * This lab is just a practice in bitwise operations. Though all of the return and arg types are ints, - * pretend that they're bytes. That is, all of the tests are passing in values that are only 8 bits. - * Use only bitwise operations (&, |, ^, ~, >>>, >>, <<) and return. Nothing else. - * - * The tests are written for you, so look at those for inspiration on how to solve these. All you need to do is write - * the functions to make the tests pass. - */ + public class LightSwitcher { +// ~ (not) +// & (and) +// | (or) +// ^ (xor) +// << (bitshift left) +// >> (bitshift right extending sign bit) +// >>> (bitshift right ignoring sign bit) /** * Take a given input of switches, and the switches to turn on, and return the new state of the switches. * 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,7 +30,7 @@ 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); } /** @@ -38,7 +38,7 @@ public static int turnOffSwitches(int switches, int switchesToTurnOff) { * Remember to use bit notation and a bit operator. */ public static int turnOffAllSwitches(int switches) { - + return switches & 0b00000000; } /** @@ -47,7 +47,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 +55,8 @@ 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,22 +65,23 @@ public static int flipAllSwitches(int switches) { * So, a byte reads 76543210 */ public static int getSwitchPositionAt(int switches, int position) { - + return switches >>> position & 0b00000001; } /** * Move all the the bits to the right `count` places. */ public static int moveRightBy(int switches, int count) { - + return switches >> count; } /** * Move all the the bits to the left `count` places. * 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; } /** @@ -90,4 +92,5 @@ public static String viewSwitches(int switches) { return String.format("%8s", Integer.toBinaryString((switches & 0b11111111))).replace(' ', '0'); } + } diff --git a/src/test/java/LightSwitcherTest.java b/src/test/java/LightSwitcherTest.java index 071bfd5..76491c6 100644 --- a/src/test/java/LightSwitcherTest.java +++ b/src/test/java/LightSwitcherTest.java @@ -104,6 +104,7 @@ public void testGetSwitchPositionAt() { Assert.assertEquals(1, LightSwitcher.getSwitchPositionAt(0b11111110, 7)); } + @Test public void testMoveRightBy() { int result1 = LightSwitcher.moveRightBy(baseByte, 1);