Skip to content

JS bitwise #1

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
19 changes: 9 additions & 10 deletions src/main/java/LightSwitcher.java
Original file line number Diff line number Diff line change
Expand Up @@ -13,15 +13,14 @@ 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;
}

/**
* 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 +29,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 +46,15 @@ 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 >>> 2;
}

/**
Expand All @@ -64,22 +63,22 @@ public static int flipAllSwitches(int switches) {
* So, a byte reads 76543210
*/
public static int getSwitchPositionAt(int switches, int position) {

return -1;
}

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

/**
Expand Down
1 change: 1 addition & 0 deletions src/test/java/LightSwitcherTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ public void testTurnOnSwitches() {
Assert.assertEquals(result2, expected2);
Assert.assertEquals(result3, expected3);
Assert.assertEquals(result4, expected4);

}

@Test
Expand Down