Skip to content
Draft
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
13 changes: 11 additions & 2 deletions config.json
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,15 @@
"basics"
],
"status": "wip"
},
{
"slug": "secrets",
"name": "secrets",
"uuid": "eeb45ce0-2caa-4283-b374-f086a9627da2",
"concepts": [
"bitwise-operations"
],
"prerequisites": []
}
],
"practice": [
Expand Down Expand Up @@ -1196,15 +1205,15 @@
"practices": [],
"prerequisites": [],
"difficulty": 6,
"status": "deprecated",
"topics": [
"conditionals",
"games",
"integers",
"lists",
"matrices",
"strings"
],
"status": "deprecated"
]
},
{
"slug": "rail-fence-cipher",
Expand Down
18 changes: 18 additions & 0 deletions exercises/concept/secrets/.docs/hints.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
# Hints

## 1. Shift back the bits

- There are two operators for shifting to the right, but only one will always insert a 0.

## 2. Set some bits

- One of the bit wise operators will always set a bit to 1 where the bits in both values are 1.

## 3. Flip specific bits

- There is an bit operation that will flip a bit where the mask is 1.

## 4. Clear specific bits

- One of the bit operations clears bits where the bit in the mask is 0.
- But you may need to combine it with another operator to clear bits where the mask is 1.
58 changes: 58 additions & 0 deletions exercises/concept/secrets/.docs/instructions.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
# Instructions

Your friend has just sent you a message with an important secret.
Not wanting to make it easy for others to read it, the message was encrypted by performing a series of bit manipulations.
You will need to write the functions to help decrypt the message.

## 1. Shift back the bits

The first step in decrypting the message is to undo the shifting from the encryption process by shifting the bits back to the right.
There will be further steps in the decryption process that assume 0s are inserted from the left hand side.

Implement the `shiftBack` function that takes a value and the number of places to shift and performs the shift.

```kotlin
shiftBack(0b1001, 2)
# => 0b0010
```

## 2. Set some bits

Next, there are some bits that need to be set to 1.

Implement the `setBits` function that takes a value and a mask and returns the result of setting the bits in value to 1.
A bit from value should be set to 1 where the bit in the mask is also 1.
All other bits should be kept unchanged.

```kotlin
setBits(0b0110, 0b0101);
# => 0b0111
```

## 3. Flip specific bits

Some bits are flipped during encryption.
They will need to be flipped back to decrypt the message.

Implement the `flipBits` function that takes a value and the mask.
The mask indicates which bits in the value to flip.
If the bit is 1 in mask, the bit is flipped in the value.
All other bits are kept unchanged.

```java
flipBits(0b1100, 0b0101);
# => 0b1001
```

## 4. Clear specific bits

Lastly, there are also certain bits that always decrypt to 0.

Implement the `clearBits` function that takes a value and a mask.
The bits in the `value` should be set to 0 where the bit in the mask is 1.
All other bits should be kept unchanged.

```java
clearBits(0b0110, 0b0101);
# => 0b0010
```
21 changes: 21 additions & 0 deletions exercises/concept/secrets/.meta/config.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
{
"authors": [
"kahgoh"
],
"files": {
"solution": [
"src/main/kotlin/Secrets.kt"
],
"test": [
"src/test/kotlin/SecretsTest.kt"
],
"exemplar": [
".meta/src/reference/kotlin/Secrets.kt"
]
},
"forked_from": [
"crystal/secrets"
],
"icon": "secrets",
"blurb": "Learn about bit manipulation by writing a program to decrypt a message."
}
15 changes: 15 additions & 0 deletions exercises/concept/secrets/.meta/src/reference/kotlin/Secrets.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
fun shiftBack(value: Int, amount: Int): Int {
return value ushr amount
}

fun setBits(value: Int, mask: Int): Int {
return value or mask
}

fun flipBits(value: Int, mask: Int): Int {
return value xor mask
}

fun clearBits(value: Int, mask: Int): Int {
return value and mask.inv()
}
23 changes: 23 additions & 0 deletions exercises/concept/secrets/build.gradle.kts
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
import org.gradle.api.tasks.testing.logging.TestExceptionFormat

plugins {
kotlin("jvm")
}

repositories {
mavenCentral()
}

dependencies {
implementation(kotlin("stdlib-jdk8"))

testImplementation("junit:junit:4.13.2")
testImplementation(kotlin("test-junit"))
}

tasks.withType<Test> {
testLogging {
exceptionFormat = TestExceptionFormat.FULL
events("passed", "failed", "skipped")
}
}
Binary file not shown.
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
distributionBase=GRADLE_USER_HOME
distributionPath=wrapper/dists
distributionUrl=https\://services.gradle.org/distributions/gradle-8.4-bin.zip
networkTimeout=10000
validateDistributionUrl=true
zipStoreBase=GRADLE_USER_HOME
zipStorePath=wrapper/dists
Loading
Loading