Skip to content

done bitwise micro #6

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
37 changes: 20 additions & 17 deletions src/main/java/LightSwitcher.java
Original file line number Diff line number Diff line change
@@ -1,27 +1,27 @@
/**
* 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;
}

/**
* Take a given input of switches and turn them all to on.
* Remember to use bit notation (0bxxxxxxxx) and a bit operator.
*/
public static int turnOnAllSwitches(int switches) {

return switches | 0b11111111;
}

/**
Expand All @@ -30,15 +30,15 @@ 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) {

return switches & 0b00000000;
}

/**
Expand All @@ -47,15 +47,16 @@ 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;
}

/**
* Take a given input of switches and flip them all.
* 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;
}

/**
Expand All @@ -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;
}

/**
Expand All @@ -90,4 +92,5 @@ public static String viewSwitches(int switches) {

return String.format("%8s", Integer.toBinaryString((switches & 0b11111111))).replace(' ', '0');
}

}
1 change: 1 addition & 0 deletions src/test/java/LightSwitcherTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,7 @@ public void testGetSwitchPositionAt() {
Assert.assertEquals(1, LightSwitcher.getSwitchPositionAt(0b11111110, 7));
}


@Test
public void testMoveRightBy() {
int result1 = LightSwitcher.moveRightBy(baseByte, 1);
Expand Down