Skip to content

Commit

Permalink
Tidy up bit manipulation concept
Browse files Browse the repository at this point in the history
- Fix references to Crystal's Int class
- Fix links to Crystal docs (no longer linking to a specific version)
- Update the test to match instructions
  • Loading branch information
kahgoh committed Nov 14, 2023
1 parent 0d4947a commit ff0ce14
Show file tree
Hide file tree
Showing 7 changed files with 30 additions and 32 deletions.
4 changes: 2 additions & 2 deletions concepts/bit-manipulation/about.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# Bit Manipulation

Crystal has bitwise operators for manipulating [`Integers`][integers] at the binary level.
Crystal has bitwise operators for manipulating [`Int`][ints] at the binary level.

## Shift operators

Expand Down Expand Up @@ -96,6 +96,6 @@ Unlike the other binary operators, this is a unary operator, operating on only o
# => 0b0001_1101
```

[integers]: https://crystal-lang.org/api/Int.html
[ints]: https://crystal-lang.org/api/Int.html
[shift]: https://crystal-lang.org/reference/syntax_and_semantics/operators.html#shifts
[binary]: https://crystal-lang.org/reference/syntax_and_semantics/operators.html#binary
4 changes: 2 additions & 2 deletions concepts/bit-manipulation/introduction.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# Bit Manipulation

Crystal has bitwise operators for manipulating [`Integers`][integers] at the binary level.
Crystal has bitwise operators for manipulating [`Int`][ints] at the binary level.

## Shift operators

Expand Down Expand Up @@ -96,6 +96,6 @@ Unlike the other binary operators, this is a unary operator, operating on only o
# => 0b0001_1101
```

[integers]: https://crystal-lang.org/api/Int.html
[ints]: https://crystal-lang.org/api/Int.html
[shift]: https://crystal-lang.org/reference/syntax_and_semantics/operators.html#shifts
[binary]: https://crystal-lang.org/reference/syntax_and_semantics/operators.html#binary
6 changes: 3 additions & 3 deletions concepts/bit-manipulation/links.json
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
[
{
"url": "https://crystal-lang.org/reference/1.10/syntax_and_semantics/operators.html#binary",
"url": "https://crystal-lang.org/reference/syntax_and_semantics/operators.html#binary",
"description": "Crystal docs: List of operators - Binary"
},
{
"url": "https://crystal-lang.org/reference/1.10/syntax_and_semantics/operators.html#shifts",
"url": "https://crystal-lang.org/reference/syntax_and_semantics/operators.html#shifts",
"description": "Crystal docs: List of operators - Shifts"
},
{
"url": "https://crystal-lang.org/reference/1.10/syntax_and_semantics/operators.html#other-unary-operators",
"url": "https://crystal-lang.org/reference/syntax_and_semantics/operators.html#other-unary-operators",
"description": "Crystal docs: List of operators - Unary"
}
]
4 changes: 2 additions & 2 deletions exercises/concept/secrets/.docs/introduction.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# Bit Manipulation

Crystal has bitwise operators for manipulating [`Integers`][integers] at the binary level.
Crystal has bitwise operators for manipulating [`Int`][ints] at the binary level.

## Shift operators

Expand Down Expand Up @@ -96,6 +96,6 @@ Unlike the other binary operators, this is a unary operator, operating on only o
# => 0b0001_1101
```

[integers]: https://crystal-lang.org/api/Int.html
[ints]: https://crystal-lang.org/api/Int.html
[shift]: https://crystal-lang.org/reference/syntax_and_semantics/operators.html#shifts
[binary]: https://crystal-lang.org/reference/syntax_and_semantics/operators.html#binary
8 changes: 4 additions & 4 deletions exercises/concept/secrets/.meta/src/exemplar.cr
Original file line number Diff line number Diff line change
@@ -1,17 +1,17 @@
module Secrets
def shift_back(value : UInt8, numberOfPlaces : UInt8) : UInt8
def self.shift_back(value : UInt8, numberOfPlaces : UInt8) : UInt8
value << numberOfPlaces
end

def apply_mask(value : UInt8, mask : UInt8) : UInt8
def self.apply_mask(value : UInt8, mask : UInt8) : UInt8
value & mask
end

def set_bits(value1 : UInt8, value2 : UInt8) : UInt8
def self.set_bits(value1 : UInt8, value2 : UInt8) : UInt8
value1 | value2
end

def reverse_xor(value : UInt8, agreedValue : UInt8) : UInt8
def self.reverse_xor(value : UInt8, agreedValue : UInt8) : UInt8
value ^ ~agreedValue
end
end
28 changes: 13 additions & 15 deletions exercises/concept/secrets/spec/secrets_spec.cr
Original file line number Diff line number Diff line change
@@ -1,66 +1,64 @@
require "spec"
require "../src/*"

include Secrets

describe "Secrets" do
describe "shift_back" do
it "should be able to shift 0 places" do
shift_back(15, 0).should eq 15
Secrets.shift_back(15, 0).should eq 15
end

it "should be able to shift 1 place to the left" do
shift_back(7, 1).should eq 14
Secrets.shift_back(7, 1).should eq 14
end

it "should be able to shift 4 places to the left" do
shift_back(5, 4).should eq 80
Secrets.shift_back(5, 4).should eq 80
end

it "should be able to shift bits off" do
shift_back(96, 4).should eq 0
Secrets.shift_back(96, 4).should eq 0
end
end

describe "apply_mask" do
it "should be able to AND value with mask bits all 1" do
apply_mask(101, 255).should eq 101
Secrets.apply_mask(101, 255).should eq 101
end

it "should be able to AND value with mask bits all 0" do
apply_mask(101, 0).should eq 0
Secrets.apply_mask(101, 0).should eq 0
end

it "should be able to AND value with some bits 1" do
apply_mask(62, 85).should eq 20
Secrets.apply_mask(62, 85).should eq 20
end
end

describe "set_bits" do
it "should be able to OR value with 0s" do
set_bits(107, 0).should eq 107
Secrets.set_bits(107, 0).should eq 107
end

it "should be able to OR with with 1s" do
set_bits(107, 255).should eq 255
Secrets.set_bits(107, 255).should eq 255
end

it "should be able to set some bits" do
set_bits(62, 85).should eq 127
Secrets.set_bits(62, 85).should eq 127
end
end

describe "reverse_xor" do
it "should be able to reverse with all 1s" do
reverse_xor(106, 255).should eq 106
Secrets.reverse_xor(106, 255).should eq 106
end

it "should be able to reverse with all 0s" do
reverse_xor(106, 0).should eq 149
Secrets.reverse_xor(106, 0).should eq 149
end

it "should be able to reverse with mix of 1s and 0s" do
reverse_xor(62, 85).should eq 148
Secrets.reverse_xor(62, 85).should eq 148
end
end
end
8 changes: 4 additions & 4 deletions exercises/concept/secrets/src/secrets.cr
Original file line number Diff line number Diff line change
@@ -1,17 +1,17 @@
module Secrets
def shift_back(value : UInt8, numberOfPlaces : UInt8) : UInt8
def self.shift_back(value : UInt8, numberOfPlaces : UInt8) : UInt8
raise "Please implement the Secrets::shift_back method"
end

def apply_mask(value : UInt8, mask : UInt8) : UInt8
def self.apply_mask(value : UInt8, mask : UInt8) : UInt8
raise "Please implement the Secrets::apply_mask method"
end

def set_bits(value1 : UInt8, value2 : UInt8) : UInt8
def self.set_bits(value1 : UInt8, value2 : UInt8) : UInt8
raise "Please implement the Secrets::set_bits method"
end

def reverse_xor(value : UInt8, agreedValue : UInt8) : UInt8
def self.reverse_xor(value : UInt8, agreedValue : UInt8) : UInt8
raise "Please implement the Secrets::reverse_xor method"
end
end

0 comments on commit ff0ce14

Please sign in to comment.