From 90ed84e58f028eabb9a317adeaf83b8c81fec2ae Mon Sep 17 00:00:00 2001 From: Stewart Murrie Date: Sat, 2 Nov 2024 14:55:51 -0700 Subject: [PATCH 01/26] Initial stub. --- concepts/bitwise_operations/.meta/config.json | 9 +++++++++ concepts/bitwise_operations/about.md | 0 concepts/bitwise_operations/introduction.md | 0 concepts/bitwise_operations/links.json | 10 ++++++++++ 4 files changed, 19 insertions(+) create mode 100644 concepts/bitwise_operations/.meta/config.json create mode 100644 concepts/bitwise_operations/about.md create mode 100644 concepts/bitwise_operations/introduction.md create mode 100644 concepts/bitwise_operations/links.json diff --git a/concepts/bitwise_operations/.meta/config.json b/concepts/bitwise_operations/.meta/config.json new file mode 100644 index 00000000..a9dd41c3 --- /dev/null +++ b/concepts/bitwise_operations/.meta/config.json @@ -0,0 +1,9 @@ +{ + "authors": [ + "stewartmurrie" + ], + "contributors": [ + "" + ], + "blurb": "Learn how to use integer bitwise operations in Elm" +} diff --git a/concepts/bitwise_operations/about.md b/concepts/bitwise_operations/about.md new file mode 100644 index 00000000..e69de29b diff --git a/concepts/bitwise_operations/introduction.md b/concepts/bitwise_operations/introduction.md new file mode 100644 index 00000000..e69de29b diff --git a/concepts/bitwise_operations/links.json b/concepts/bitwise_operations/links.json new file mode 100644 index 00000000..eddbc1b5 --- /dev/null +++ b/concepts/bitwise_operations/links.json @@ -0,0 +1,10 @@ +[ + { + "url": "https://package.elm-lang.org/packages/elm/core/latest/Bitwise", + "description": "Bitwise module" + }, + { + "url": "https://guide.elm-lang.org/appendix/types_as_bits", + "description": "Documentation on how Elm represents types as bits" + } +] \ No newline at end of file From 906a767987cfe5b2e34a8a5c9ba943eb3be2f9b4 Mon Sep 17 00:00:00 2001 From: Stewart Murrie Date: Sat, 2 Nov 2024 14:58:08 -0700 Subject: [PATCH 02/26] Adds stub for bitwise concept exericse (secrets) --- config.json | 7 +++++++ exercises/concept/secrets/.docs/instructions.md | 0 exercises/concept/secrets/.docs/introduction.md | 0 exercises/concept/secrets/.meta/Exemplar.elm | 0 exercises/concept/secrets/.meta/config.json | 15 +++++++++++++++ exercises/concept/secrets/src/Secrets.elm | 0 exercises/concept/secrets/tests/Tests.elm | 0 7 files changed, 22 insertions(+) create mode 100644 exercises/concept/secrets/.docs/instructions.md create mode 100644 exercises/concept/secrets/.docs/introduction.md create mode 100644 exercises/concept/secrets/.meta/Exemplar.elm create mode 100644 exercises/concept/secrets/.meta/config.json create mode 100644 exercises/concept/secrets/src/Secrets.elm create mode 100644 exercises/concept/secrets/tests/Tests.elm diff --git a/config.json b/config.json index 823ab69c..27eb6b2d 100644 --- a/config.json +++ b/config.json @@ -339,6 +339,13 @@ "booleans" ], "status": "beta" + }, + { + "slug": "secrets", + "name": "secrets", + "uuid": "89a899b1-8cb2-4099-98c9-468723728a28", + "concepts": [], + "prerequisites": [] } ], "practice": [ diff --git a/exercises/concept/secrets/.docs/instructions.md b/exercises/concept/secrets/.docs/instructions.md new file mode 100644 index 00000000..e69de29b diff --git a/exercises/concept/secrets/.docs/introduction.md b/exercises/concept/secrets/.docs/introduction.md new file mode 100644 index 00000000..e69de29b diff --git a/exercises/concept/secrets/.meta/Exemplar.elm b/exercises/concept/secrets/.meta/Exemplar.elm new file mode 100644 index 00000000..e69de29b diff --git a/exercises/concept/secrets/.meta/config.json b/exercises/concept/secrets/.meta/config.json new file mode 100644 index 00000000..5c3566dc --- /dev/null +++ b/exercises/concept/secrets/.meta/config.json @@ -0,0 +1,15 @@ +{ + "authors": ["stewartmurrie"], + "files": { + "solution": [ + "src/Secrets.elm" + ], + "test": [ + "tests/Tests.elm" + ], + "exemplar": [ + ".meta/Exemplar.elm" + ] + }, + "blurb": "Learn about bit manipulation and anonymous functions by writing the software for an encryption device." +} diff --git a/exercises/concept/secrets/src/Secrets.elm b/exercises/concept/secrets/src/Secrets.elm new file mode 100644 index 00000000..e69de29b diff --git a/exercises/concept/secrets/tests/Tests.elm b/exercises/concept/secrets/tests/Tests.elm new file mode 100644 index 00000000..e69de29b From c92597016259e4904bd94541236b744786f2a3b6 Mon Sep 17 00:00:00 2001 From: Stewart Murrie Date: Sat, 2 Nov 2024 16:51:39 -0700 Subject: [PATCH 03/26] Adds first draft of concept --- concepts/bitwise_operations/about.md | 126 +++++++++++++++++++++++++++ 1 file changed, 126 insertions(+) diff --git a/concepts/bitwise_operations/about.md b/concepts/bitwise_operations/about.md index e69de29b..9374691f 100644 --- a/concepts/bitwise_operations/about.md +++ b/concepts/bitwise_operations/about.md @@ -0,0 +1,126 @@ +# Bitwise Operations + +Bitwise operations allow us to manipulate individual digits within binary numbers. These operations are fundamental in computing, providing an efficient way to perform low-level tasks, such as controlling specific bits in memory, optimizing mathematical calculations, and encoding/decoding data. + +## Understanding 32-bit Integers in Elm +In Elm, integers are stored as 32-bit signed integers using [two's complement](https://en.wikipedia.org/wiki/Two%27s_complement) representation, meaning they use 32 binary digits (_bits_) to store each integer value. This bit limit affects the range of integers that Elm can represent directly: + +Positive Range: 0 to 2^31 - 1 (or 0 to 2,147,483,647) +Negative Range: -2^31 to -1 (or -2,147,483,648 to -1) + +For example, the integer `5` is represented in binary as `00000000000000000000000000000101`, although usually we ignore the leading zeros and just say `101`. + +## Bitwise operations +Elm provides several bitwise operators in its [Bitwise module](https://package.elm-lang.org/packages/elm/core/latest/Bitwise) + +## Basic operations + +### and +`and` combines two numbers by keeping only the bits that are `1` in both. For example: + +```elm +Bitwise.and 21 13 --> 5 +-- 21 = 10101 +-- 13 = 01101 +-- and = 00101 = 5 +``` + This is useful for checking to see if an individual bit is set. For example, to check if the 4th bit of a number is set to `1`, `and` it with `01000` (`8` in decimal) and see if the result is non-zero: + + ```elm + Bitwise.and 13 8 --> 8 + -- 13 = 01101 + -- 8 = 01000 + -- and = 01000 = 8 + + Bitwise.and 21 8 -> 0 + -- 21 = 10101 + -- 8 = 01000 + -- and = 00000 = 0 + ``` + +### or +`or` combines two numbers by setting each bit to `1` if it is `1` in either or both numbers. + +```elm +Bitwise.or 21 13 --> 29 +-- 21 = 10101 +-- 13 = 01101 +-- or = 11101 = 29 +``` +This is useful for setting a specific bit to `1`. For example, to set the 2nd bit in `10101`: + +```elm +Bitwise.or 21 2 --> 23 +-- 21 = 10101 +-- 2 = 00010 +-- or = 10111 = 23 +``` + +### Exclusive-or (xor) +`xor` combines two numbers by setting each bit to `1` if it is `1` in one number but `0` in the other. + +```elm +Bitwise.xor 21 13 --> 24 +-- 21 = 10101 +-- 13 = 01101 +-- xor = 11000 = 24 +``` +This is useful for flipping a bit to its opposite value: + +```elm +Bitwise.xor 21 4 --> 17 +-- 21 = 10101 +-- 4 = 00100 +-- xor = 10001 = 17 + +Bitwise.xor 17 4 --> 21 +-- 17 = 10001 +-- 4 = 00100 +-- xor = 10101 = 21 +``` + +### Complement +`complement` inverts each bit of a number (`0` becomes `1`, `1` becomes `0`). + +Note that this will result in positive numbers becoming negative, and negative numbers becoming positive. This is because negative numbers in binary are represented with `1` in the left-most position. + +```elm +Bitwise.complement 21 --> -22 +-- 21 = 00000000000000000000000000010101 +-- complement = 11111111111111111111111111101010 = -22 +``` + +## Bit shifting +The following operators move bits left or right by a specified number of positions, effectively multiplying or dividing by powers of 2. + +`shiftLeftBy` moves bits to the left, filling in with `0` from the right-hand side. For example, to shift `21` left by 3 places: + +```elm +Bitwise.shiftLeftBy 3 21 --> 168 +-- 21 = 10101 +-- shiftLeftBy 3 = 10101000 = 168 +``` +This is the same as saying `21 * 2^3 = 21 * 2 * 2 * 2 = 168` + +`shiftRightBy`: Moves bits to the right: +```elm +Bitwise.shiftRightBy 2 21 --> 5 +-- 21 = 10101 +-- shiftRightBy 2 = 00101 = 5 +``` +Shifiting to the right by 2 places is the same as integer division by 4. + +Note that this function duplicates whatever value is in the leftmost bit. So negative numbers will stay negative: + +```elm +Bitwise.shiftRightBy 3 -21 --> -6 +-- -21 = 111...10101 +-- shiftRightBy 3 = 111...11101 = -6 +``` + +If you want to shift right and fill in with zeros, use `shiftRightZfBy`: +```elm +Bitwise.shiftRightZfBy 3 -21 --> 1073741818 +-- -21 = 111...10101 +-- shiftRightBy 3 = 00111...11101 = 1073741818 +``` \ No newline at end of file From 52770b9dda4bb112d93e1728f495b584e04d592e Mon Sep 17 00:00:00 2001 From: Stewart Murrie Date: Sat, 2 Nov 2024 17:04:41 -0700 Subject: [PATCH 04/26] Added notes on masking --- concepts/bitwise_operations/about.md | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/concepts/bitwise_operations/about.md b/concepts/bitwise_operations/about.md index 9374691f..fad1ad62 100644 --- a/concepts/bitwise_operations/about.md +++ b/concepts/bitwise_operations/about.md @@ -14,6 +14,7 @@ For example, the integer `5` is represented in binary as `0000000000000000000000 Elm provides several bitwise operators in its [Bitwise module](https://package.elm-lang.org/packages/elm/core/latest/Bitwise) ## Basic operations +Modifying individual bits of a number is called _masking_. A _mask_ is a number where specific bits have been set in a particular way to manipulate another number using bitwise operators such as `and`, `or`, and `xor`. ### and `and` combines two numbers by keeping only the bits that are `1` in both. For example: @@ -24,7 +25,7 @@ Bitwise.and 21 13 --> 5 -- 13 = 01101 -- and = 00101 = 5 ``` - This is useful for checking to see if an individual bit is set. For example, to check if the 4th bit of a number is set to `1`, `and` it with `01000` (`8` in decimal) and see if the result is non-zero: + This is useful for checking to see if an individual bit is set. For example, to check if the 4th bit of a number is set to `1`, `and` it with a mask of `01000` (`8` in decimal) and see if the result is non-zero: ```elm Bitwise.and 13 8 --> 8 @@ -37,6 +38,7 @@ Bitwise.and 21 13 --> 5 -- 8 = 01000 -- and = 00000 = 0 ``` + ### or `or` combines two numbers by setting each bit to `1` if it is `1` in either or both numbers. @@ -47,7 +49,7 @@ Bitwise.or 21 13 --> 29 -- 13 = 01101 -- or = 11101 = 29 ``` -This is useful for setting a specific bit to `1`. For example, to set the 2nd bit in `10101`: +This is useful for setting a specific bit to `1`. For example, to set the 2nd bit in `10101`, `or` it with the mask `00010`: ```elm Bitwise.or 21 2 --> 23 From 82e04241a0502a1d891f229781a3f3a47051a990 Mon Sep 17 00:00:00 2001 From: Stewart Murrie Date: Sat, 2 Nov 2024 17:16:39 -0700 Subject: [PATCH 05/26] Adds introduction.md (a copy of about.md) --- concepts/bitwise_operations/about.md | 46 ++++--- concepts/bitwise_operations/introduction.md | 142 ++++++++++++++++++++ 2 files changed, 172 insertions(+), 16 deletions(-) diff --git a/concepts/bitwise_operations/about.md b/concepts/bitwise_operations/about.md index fad1ad62..c0191450 100644 --- a/concepts/bitwise_operations/about.md +++ b/concepts/bitwise_operations/about.md @@ -1,8 +1,9 @@ # Bitwise Operations -Bitwise operations allow us to manipulate individual digits within binary numbers. These operations are fundamental in computing, providing an efficient way to perform low-level tasks, such as controlling specific bits in memory, optimizing mathematical calculations, and encoding/decoding data. +Bitwise operations allow us to manipulate individual digits within binary numbers. These operations are fundamental in computing, providing an efficient way to perform low-level tasks, such as controlling specific bits in memory, optimizing mathematical calculations, encryption, communications, and encoding/decoding data. ## Understanding 32-bit Integers in Elm + In Elm, integers are stored as 32-bit signed integers using [two's complement](https://en.wikipedia.org/wiki/Two%27s_complement) representation, meaning they use 32 binary digits (_bits_) to store each integer value. This bit limit affects the range of integers that Elm can represent directly: Positive Range: 0 to 2^31 - 1 (or 0 to 2,147,483,647) @@ -11,12 +12,15 @@ Negative Range: -2^31 to -1 (or -2,147,483,648 to -1) For example, the integer `5` is represented in binary as `00000000000000000000000000000101`, although usually we ignore the leading zeros and just say `101`. ## Bitwise operations + Elm provides several bitwise operators in its [Bitwise module](https://package.elm-lang.org/packages/elm/core/latest/Bitwise) ## Basic operations -Modifying individual bits of a number is called _masking_. A _mask_ is a number where specific bits have been set in a particular way to manipulate another number using bitwise operators such as `and`, `or`, and `xor`. + +Modifying individual bits of a number is called _masking_. A _mask_ is a number where specific bits have been set in a particular way to manipulate another number using bitwise operators such as `and`, `or`, and `xor`. ### and + `and` combines two numbers by keeping only the bits that are `1` in both. For example: ```elm @@ -25,22 +29,23 @@ Bitwise.and 21 13 --> 5 -- 13 = 01101 -- and = 00101 = 5 ``` - This is useful for checking to see if an individual bit is set. For example, to check if the 4th bit of a number is set to `1`, `and` it with a mask of `01000` (`8` in decimal) and see if the result is non-zero: - ```elm - Bitwise.and 13 8 --> 8 - -- 13 = 01101 - -- 8 = 01000 - -- and = 01000 = 8 +This is useful for checking to see if an individual bit is set. For example, to check if the 4th bit of a number is set to `1`, `and` it with a mask of `01000` (`8` in decimal) and see if the result is non-zero: + +```elm +Bitwise.and 13 8 --> 8 +-- 13 = 01101 +-- 8 = 01000 +-- and = 01000 = 8 - Bitwise.and 21 8 -> 0 - -- 21 = 10101 - -- 8 = 01000 - -- and = 00000 = 0 - ``` - +Bitwise.and 21 8 -> 0 +-- 21 = 10101 +-- 8 = 01000 +-- and = 00000 = 0 +``` ### or + `or` combines two numbers by setting each bit to `1` if it is `1` in either or both numbers. ```elm @@ -49,6 +54,7 @@ Bitwise.or 21 13 --> 29 -- 13 = 01101 -- or = 11101 = 29 ``` + This is useful for setting a specific bit to `1`. For example, to set the 2nd bit in `10101`, `or` it with the mask `00010`: ```elm @@ -59,6 +65,7 @@ Bitwise.or 21 2 --> 23 ``` ### Exclusive-or (xor) + `xor` combines two numbers by setting each bit to `1` if it is `1` in one number but `0` in the other. ```elm @@ -67,6 +74,7 @@ Bitwise.xor 21 13 --> 24 -- 13 = 01101 -- xor = 11000 = 24 ``` + This is useful for flipping a bit to its opposite value: ```elm @@ -82,7 +90,8 @@ Bitwise.xor 17 4 --> 21 ``` ### Complement -`complement` inverts each bit of a number (`0` becomes `1`, `1` becomes `0`). + +`complement` inverts each bit of a number (`0` becomes `1`, `1` becomes `0`). Note that this will result in positive numbers becoming negative, and negative numbers becoming positive. This is because negative numbers in binary are represented with `1` in the left-most position. @@ -93,6 +102,7 @@ Bitwise.complement 21 --> -22 ``` ## Bit shifting + The following operators move bits left or right by a specified number of positions, effectively multiplying or dividing by powers of 2. `shiftLeftBy` moves bits to the left, filling in with `0` from the right-hand side. For example, to shift `21` left by 3 places: @@ -102,14 +112,17 @@ Bitwise.shiftLeftBy 3 21 --> 168 -- 21 = 10101 -- shiftLeftBy 3 = 10101000 = 168 ``` + This is the same as saying `21 * 2^3 = 21 * 2 * 2 * 2 = 168` `shiftRightBy`: Moves bits to the right: + ```elm Bitwise.shiftRightBy 2 21 --> 5 -- 21 = 10101 -- shiftRightBy 2 = 00101 = 5 ``` + Shifiting to the right by 2 places is the same as integer division by 4. Note that this function duplicates whatever value is in the leftmost bit. So negative numbers will stay negative: @@ -121,8 +134,9 @@ Bitwise.shiftRightBy 3 -21 --> -6 ``` If you want to shift right and fill in with zeros, use `shiftRightZfBy`: + ```elm Bitwise.shiftRightZfBy 3 -21 --> 1073741818 -- -21 = 111...10101 -- shiftRightBy 3 = 00111...11101 = 1073741818 -``` \ No newline at end of file +``` diff --git a/concepts/bitwise_operations/introduction.md b/concepts/bitwise_operations/introduction.md index e69de29b..546e89d4 100644 --- a/concepts/bitwise_operations/introduction.md +++ b/concepts/bitwise_operations/introduction.md @@ -0,0 +1,142 @@ +# About Bitwise Operations + +Bitwise operations allow us to manipulate individual digits within binary numbers. These operations are fundamental in computing, providing an efficient way to perform low-level tasks, such as controlling specific bits in memory, optimizing mathematical calculations, encryption, communications, and encoding/decoding data. + +## Understanding 32-bit Integers in Elm + +In Elm, integers are stored as 32-bit signed integers using [two's complement](https://en.wikipedia.org/wiki/Two%27s_complement) representation, meaning they use 32 binary digits (_bits_) to store each integer value. This bit limit affects the range of integers that Elm can represent directly: + +Positive Range: 0 to 2^31 - 1 (or 0 to 2,147,483,647) +Negative Range: -2^31 to -1 (or -2,147,483,648 to -1) + +For example, the integer `5` is represented in binary as `00000000000000000000000000000101`, although usually we ignore the leading zeros and just say `101`. + +## Bitwise operations + +Elm provides several bitwise operators in its [Bitwise module](https://package.elm-lang.org/packages/elm/core/latest/Bitwise) + +## Basic operations + +Modifying individual bits of a number is called _masking_. A _mask_ is a number where specific bits have been set in a particular way to manipulate another number using bitwise operators such as `and`, `or`, and `xor`. + +### and + +`and` combines two numbers by keeping only the bits that are `1` in both. For example: + +```elm +Bitwise.and 21 13 --> 5 +-- 21 = 10101 +-- 13 = 01101 +-- and = 00101 = 5 +``` + +This is useful for checking to see if an individual bit is set. For example, to check if the 4th bit of a number is set to `1`, `and` it with a mask of `01000` (`8` in decimal) and see if the result is non-zero: + +```elm +Bitwise.and 13 8 --> 8 +-- 13 = 01101 +-- 8 = 01000 +-- and = 01000 = 8 + +Bitwise.and 21 8 -> 0 +-- 21 = 10101 +-- 8 = 01000 +-- and = 00000 = 0 +``` + +### or + +`or` combines two numbers by setting each bit to `1` if it is `1` in either or both numbers. + +```elm +Bitwise.or 21 13 --> 29 +-- 21 = 10101 +-- 13 = 01101 +-- or = 11101 = 29 +``` + +This is useful for setting a specific bit to `1`. For example, to set the 2nd bit in `10101`, `or` it with the mask `00010`: + +```elm +Bitwise.or 21 2 --> 23 +-- 21 = 10101 +-- 2 = 00010 +-- or = 10111 = 23 +``` + +### Exclusive-or (xor) + +`xor` combines two numbers by setting each bit to `1` if it is `1` in one number but `0` in the other. + +```elm +Bitwise.xor 21 13 --> 24 +-- 21 = 10101 +-- 13 = 01101 +-- xor = 11000 = 24 +``` + +This is useful for flipping a bit to its opposite value: + +```elm +Bitwise.xor 21 4 --> 17 +-- 21 = 10101 +-- 4 = 00100 +-- xor = 10001 = 17 + +Bitwise.xor 17 4 --> 21 +-- 17 = 10001 +-- 4 = 00100 +-- xor = 10101 = 21 +``` + +### Complement + +`complement` inverts each bit of a number (`0` becomes `1`, `1` becomes `0`). + +Note that this will result in positive numbers becoming negative, and negative numbers becoming positive. This is because negative numbers in binary are represented with `1` in the left-most position. + +```elm +Bitwise.complement 21 --> -22 +-- 21 = 00000000000000000000000000010101 +-- complement = 11111111111111111111111111101010 = -22 +``` + +## Bit shifting + +The following operators move bits left or right by a specified number of positions, effectively multiplying or dividing by powers of 2. + +`shiftLeftBy` moves bits to the left, filling in with `0` from the right-hand side. For example, to shift `21` left by 3 places: + +```elm +Bitwise.shiftLeftBy 3 21 --> 168 +-- 21 = 10101 +-- shiftLeftBy 3 = 10101000 = 168 +``` + +This is the same as saying `21 * 2^3 = 21 * 2 * 2 * 2 = 168` + +`shiftRightBy`: Moves bits to the right: + +```elm +Bitwise.shiftRightBy 2 21 --> 5 +-- 21 = 10101 +-- shiftRightBy 2 = 00101 = 5 +``` + +Shifiting to the right by 2 places is the same as integer division by 4. + +Note that this function duplicates whatever value is in the leftmost bit. So negative numbers will stay negative: + +```elm +Bitwise.shiftRightBy 3 -21 --> -6 +-- -21 = 111...10101 +-- shiftRightBy 3 = 111...11101 = -6 +``` + +If you want to shift right and fill in with zeros, use `shiftRightZfBy`: + +```elm +Bitwise.shiftRightZfBy 3 -21 --> 1073741818 +-- -21 = 111...10101 +-- shiftRightBy 3 = 00111...11101 = 1073741818 +``` From 77679eeb2ae8d043c8de860b7764a70434df6fe4 Mon Sep 17 00:00:00 2001 From: Stewart Murrie Date: Sat, 2 Nov 2024 17:21:42 -0700 Subject: [PATCH 06/26] Updates config and renames concept folder --- .../.meta/config.json | 0 .../{bitwise_operations => bitwise-operations}/about.md | 0 .../introduction.md | 0 .../{bitwise_operations => bitwise-operations}/links.json | 0 config.json | 6 ++++++ 5 files changed, 6 insertions(+) rename concepts/{bitwise_operations => bitwise-operations}/.meta/config.json (100%) rename concepts/{bitwise_operations => bitwise-operations}/about.md (100%) rename concepts/{bitwise_operations => bitwise-operations}/introduction.md (100%) rename concepts/{bitwise_operations => bitwise-operations}/links.json (100%) diff --git a/concepts/bitwise_operations/.meta/config.json b/concepts/bitwise-operations/.meta/config.json similarity index 100% rename from concepts/bitwise_operations/.meta/config.json rename to concepts/bitwise-operations/.meta/config.json diff --git a/concepts/bitwise_operations/about.md b/concepts/bitwise-operations/about.md similarity index 100% rename from concepts/bitwise_operations/about.md rename to concepts/bitwise-operations/about.md diff --git a/concepts/bitwise_operations/introduction.md b/concepts/bitwise-operations/introduction.md similarity index 100% rename from concepts/bitwise_operations/introduction.md rename to concepts/bitwise-operations/introduction.md diff --git a/concepts/bitwise_operations/links.json b/concepts/bitwise-operations/links.json similarity index 100% rename from concepts/bitwise_operations/links.json rename to concepts/bitwise-operations/links.json diff --git a/config.json b/config.json index 27eb6b2d..2dd7c3a5 100644 --- a/config.json +++ b/config.json @@ -1512,7 +1512,13 @@ "uuid": "357ffb6e-5a73-49b6-8b69-98ecd3c74c33", "slug": "random", "name": "Random" + }, + { + "uuid": "8737961b-c96f-4313-8737-6b88034c66d8", + "slug": "bitwise-operations", + "name": "Bitwise Operations" } + ], "key_features": [ { From cbbe48f0acd6297130b3f031cdbdcfff2021eb74 Mon Sep 17 00:00:00 2001 From: Stewart Murrie Date: Sat, 2 Nov 2024 17:26:21 -0700 Subject: [PATCH 07/26] Updates configs (lints OK now) --- concepts/bitwise-operations/.meta/config.json | 4 +--- config.json | 4 ++-- exercises/concept/secrets/.docs/hints.md | 0 3 files changed, 3 insertions(+), 5 deletions(-) create mode 100644 exercises/concept/secrets/.docs/hints.md diff --git a/concepts/bitwise-operations/.meta/config.json b/concepts/bitwise-operations/.meta/config.json index a9dd41c3..20332b45 100644 --- a/concepts/bitwise-operations/.meta/config.json +++ b/concepts/bitwise-operations/.meta/config.json @@ -2,8 +2,6 @@ "authors": [ "stewartmurrie" ], - "contributors": [ - "" - ], + "contributors": [ ], "blurb": "Learn how to use integer bitwise operations in Elm" } diff --git a/config.json b/config.json index 2dd7c3a5..96cba4ab 100644 --- a/config.json +++ b/config.json @@ -344,8 +344,8 @@ "slug": "secrets", "name": "secrets", "uuid": "89a899b1-8cb2-4099-98c9-468723728a28", - "concepts": [], - "prerequisites": [] + "concepts": ["bitwise-operations"], + "prerequisites": ["basics-1", "basics-2"] } ], "practice": [ diff --git a/exercises/concept/secrets/.docs/hints.md b/exercises/concept/secrets/.docs/hints.md new file mode 100644 index 00000000..e69de29b From 2f18e8825d269d8135de31d32a9d3f05ec29d732 Mon Sep 17 00:00:00 2001 From: Stewart Murrie Date: Sat, 2 Nov 2024 17:29:08 -0700 Subject: [PATCH 08/26] Updates allergies metadata to refer to bitwise-operations --- config.json | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/config.json b/config.json index 96cba4ab..3be694a4 100644 --- a/config.json +++ b/config.json @@ -510,11 +510,10 @@ "slug": "allergies", "name": "Allergies", "uuid": "29b5a28a-417a-4cee-ba6f-9dd942ceffaa", - "practices": [], - "prerequisites": [], + "practices": ["bitwise-operations"], + "prerequisites": ["bitwise-operations"], "difficulty": 4, "topics": [ - "bitwise_operations", "enumerations", "filtering" ] From 2d5dea3b376c9af15fe9c8912937d37bb2d5c9a6 Mon Sep 17 00:00:00 2001 From: Stewart Murrie Date: Sat, 2 Nov 2024 20:20:33 -0700 Subject: [PATCH 09/26] Adds secrets exercise docs --- exercises/concept/secrets/.docs/hints.md | 18 +++ .../concept/secrets/.docs/instructions.md | 54 +++++++ .../concept/secrets/.docs/introduction.md | 142 ++++++++++++++++++ 3 files changed, 214 insertions(+) diff --git a/exercises/concept/secrets/.docs/hints.md b/exercises/concept/secrets/.docs/hints.md index e69de29b..76949931 100644 --- a/exercises/concept/secrets/.docs/hints.md +++ b/exercises/concept/secrets/.docs/hints.md @@ -0,0 +1,18 @@ +# Hints + +## 1. Shift back the bits + +- There are two functions for shifting bits to the right, but only one will always insert a 0. + +## 2. Set some bits + +- One of the bitwise functions will always set a bit to 1 where the bits in both values are 1. + +## 3. Flip specific bits + +- There is an bitwise function that will flip a bit where the mask is 1. + +## 4. Clear specific bits + +- One of the bitwise functions clears bits where the bit in the mask is 0. +- But you may need to combine it with another function to clear bits where the mask is 1. diff --git a/exercises/concept/secrets/.docs/instructions.md b/exercises/concept/secrets/.docs/instructions.md index e69de29b..17d12a9b 100644 --- a/exercises/concept/secrets/.docs/instructions.md +++ b/exercises/concept/secrets/.docs/instructions.md @@ -0,0 +1,54 @@ +# 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 methods 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 `0`s are inserted from the left hand side. + +Implement the `shiftBack` function that takes a number of places to shift by and a value and peforms the shift. + +```elm +shiftBack 2 42 --> 10 +``` + +## 2. Set some bits + +Next, there are some bits that need to be set to `1`. + +Implement the `setBits` function that takes a mask and value 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. + +```elm +setBits 66 212 --> 64 +``` + +## 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 mask and a value. +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. + +```elm +flipBits 23 157 --> 138 +``` + +## 4. Clear specific bits + +Lastly, there are also certain bits that always decrypt to 0. + +Implement the `clearBits` functions that takes a mask and a value. +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. + +```elm +clearBits 2 15 --> 13 +``` \ No newline at end of file diff --git a/exercises/concept/secrets/.docs/introduction.md b/exercises/concept/secrets/.docs/introduction.md index e69de29b..546e89d4 100644 --- a/exercises/concept/secrets/.docs/introduction.md +++ b/exercises/concept/secrets/.docs/introduction.md @@ -0,0 +1,142 @@ +# About Bitwise Operations + +Bitwise operations allow us to manipulate individual digits within binary numbers. These operations are fundamental in computing, providing an efficient way to perform low-level tasks, such as controlling specific bits in memory, optimizing mathematical calculations, encryption, communications, and encoding/decoding data. + +## Understanding 32-bit Integers in Elm + +In Elm, integers are stored as 32-bit signed integers using [two's complement](https://en.wikipedia.org/wiki/Two%27s_complement) representation, meaning they use 32 binary digits (_bits_) to store each integer value. This bit limit affects the range of integers that Elm can represent directly: + +Positive Range: 0 to 2^31 - 1 (or 0 to 2,147,483,647) +Negative Range: -2^31 to -1 (or -2,147,483,648 to -1) + +For example, the integer `5` is represented in binary as `00000000000000000000000000000101`, although usually we ignore the leading zeros and just say `101`. + +## Bitwise operations + +Elm provides several bitwise operators in its [Bitwise module](https://package.elm-lang.org/packages/elm/core/latest/Bitwise) + +## Basic operations + +Modifying individual bits of a number is called _masking_. A _mask_ is a number where specific bits have been set in a particular way to manipulate another number using bitwise operators such as `and`, `or`, and `xor`. + +### and + +`and` combines two numbers by keeping only the bits that are `1` in both. For example: + +```elm +Bitwise.and 21 13 --> 5 +-- 21 = 10101 +-- 13 = 01101 +-- and = 00101 = 5 +``` + +This is useful for checking to see if an individual bit is set. For example, to check if the 4th bit of a number is set to `1`, `and` it with a mask of `01000` (`8` in decimal) and see if the result is non-zero: + +```elm +Bitwise.and 13 8 --> 8 +-- 13 = 01101 +-- 8 = 01000 +-- and = 01000 = 8 + +Bitwise.and 21 8 -> 0 +-- 21 = 10101 +-- 8 = 01000 +-- and = 00000 = 0 +``` + +### or + +`or` combines two numbers by setting each bit to `1` if it is `1` in either or both numbers. + +```elm +Bitwise.or 21 13 --> 29 +-- 21 = 10101 +-- 13 = 01101 +-- or = 11101 = 29 +``` + +This is useful for setting a specific bit to `1`. For example, to set the 2nd bit in `10101`, `or` it with the mask `00010`: + +```elm +Bitwise.or 21 2 --> 23 +-- 21 = 10101 +-- 2 = 00010 +-- or = 10111 = 23 +``` + +### Exclusive-or (xor) + +`xor` combines two numbers by setting each bit to `1` if it is `1` in one number but `0` in the other. + +```elm +Bitwise.xor 21 13 --> 24 +-- 21 = 10101 +-- 13 = 01101 +-- xor = 11000 = 24 +``` + +This is useful for flipping a bit to its opposite value: + +```elm +Bitwise.xor 21 4 --> 17 +-- 21 = 10101 +-- 4 = 00100 +-- xor = 10001 = 17 + +Bitwise.xor 17 4 --> 21 +-- 17 = 10001 +-- 4 = 00100 +-- xor = 10101 = 21 +``` + +### Complement + +`complement` inverts each bit of a number (`0` becomes `1`, `1` becomes `0`). + +Note that this will result in positive numbers becoming negative, and negative numbers becoming positive. This is because negative numbers in binary are represented with `1` in the left-most position. + +```elm +Bitwise.complement 21 --> -22 +-- 21 = 00000000000000000000000000010101 +-- complement = 11111111111111111111111111101010 = -22 +``` + +## Bit shifting + +The following operators move bits left or right by a specified number of positions, effectively multiplying or dividing by powers of 2. + +`shiftLeftBy` moves bits to the left, filling in with `0` from the right-hand side. For example, to shift `21` left by 3 places: + +```elm +Bitwise.shiftLeftBy 3 21 --> 168 +-- 21 = 10101 +-- shiftLeftBy 3 = 10101000 = 168 +``` + +This is the same as saying `21 * 2^3 = 21 * 2 * 2 * 2 = 168` + +`shiftRightBy`: Moves bits to the right: + +```elm +Bitwise.shiftRightBy 2 21 --> 5 +-- 21 = 10101 +-- shiftRightBy 2 = 00101 = 5 +``` + +Shifiting to the right by 2 places is the same as integer division by 4. + +Note that this function duplicates whatever value is in the leftmost bit. So negative numbers will stay negative: + +```elm +Bitwise.shiftRightBy 3 -21 --> -6 +-- -21 = 111...10101 +-- shiftRightBy 3 = 111...11101 = -6 +``` + +If you want to shift right and fill in with zeros, use `shiftRightZfBy`: + +```elm +Bitwise.shiftRightZfBy 3 -21 --> 1073741818 +-- -21 = 111...10101 +-- shiftRightBy 3 = 00111...11101 = 1073741818 +``` From 9a17e1c97e63905152446ce02c992e9b114e408b Mon Sep 17 00:00:00 2001 From: Stewart Murrie Date: Sun, 3 Nov 2024 10:50:29 -0800 Subject: [PATCH 10/26] Adds stub, example implementation, and tests --- config.json | 3 +- exercises/concept/secrets/.meta/Exemplar.elm | 21 ++++++++ exercises/concept/secrets/.meta/config.json | 5 +- exercises/concept/secrets/elm.json | 26 ++++++++++ exercises/concept/secrets/src/Secrets.elm | 17 +++++++ exercises/concept/secrets/tests/Tests.elm | 52 ++++++++++++++++++++ 6 files changed, 122 insertions(+), 2 deletions(-) create mode 100644 exercises/concept/secrets/elm.json diff --git a/config.json b/config.json index 3be694a4..657705ee 100644 --- a/config.json +++ b/config.json @@ -345,7 +345,8 @@ "name": "secrets", "uuid": "89a899b1-8cb2-4099-98c9-468723728a28", "concepts": ["bitwise-operations"], - "prerequisites": ["basics-1", "basics-2"] + "prerequisites": ["basics-1", "basics-2"], + "status": "wip" } ], "practice": [ diff --git a/exercises/concept/secrets/.meta/Exemplar.elm b/exercises/concept/secrets/.meta/Exemplar.elm index e69de29b..d38166c6 100644 --- a/exercises/concept/secrets/.meta/Exemplar.elm +++ b/exercises/concept/secrets/.meta/Exemplar.elm @@ -0,0 +1,21 @@ +module Secrets exposing (..) + +import Bitwise exposing (..) + + +shiftBack amount value = + Bitwise.shiftRightZfBy amount value + + +setBits mask value = + Bitwise.or mask value + + +flipBits mask value = + Bitwise.xor mask value + + +clearBits mask value = + mask + |> Bitwise.complement + |> Bitwise.and value diff --git a/exercises/concept/secrets/.meta/config.json b/exercises/concept/secrets/.meta/config.json index 5c3566dc..be850170 100644 --- a/exercises/concept/secrets/.meta/config.json +++ b/exercises/concept/secrets/.meta/config.json @@ -11,5 +11,8 @@ ".meta/Exemplar.elm" ] }, - "blurb": "Learn about bit manipulation and anonymous functions by writing the software for an encryption device." + "forked_from": [ + "java/secrets" + ], + "blurb": "Learn about bit manipulation by writing the software for an encryption device." } diff --git a/exercises/concept/secrets/elm.json b/exercises/concept/secrets/elm.json new file mode 100644 index 00000000..38691915 --- /dev/null +++ b/exercises/concept/secrets/elm.json @@ -0,0 +1,26 @@ +{ + "type": "application", + "source-directories": [ + "src" + ], + "elm-version": "0.19.1", + "dependencies": { + "direct": { + "elm/browser": "1.0.2", + "elm/core": "1.0.5" + }, + "indirect": { + } + }, + "test-dependencies": { + "direct": { + "elm-explorations/test": "2.1.0", + "rtfeldman/elm-iso8601-date-strings": "1.1.4" + }, + "indirect": { + "elm/bytes": "1.0.8", + "elm/html": "1.0.0", + "elm/virtual-dom": "1.0.3" + } + } +} diff --git a/exercises/concept/secrets/src/Secrets.elm b/exercises/concept/secrets/src/Secrets.elm index e69de29b..469179b7 100644 --- a/exercises/concept/secrets/src/Secrets.elm +++ b/exercises/concept/secrets/src/Secrets.elm @@ -0,0 +1,17 @@ +module Secrets exposing (..) + + +shiftBack amount value = + Debug.todo "Please implement shiftBack" + + +setBits mask value = + Debug.todo "Please implement setBits" + + +flipBits mask value = + Debug.todo "Please implement flipBits" + + +clearBits mask value = + Debug.todo "Please implement clearBits" diff --git a/exercises/concept/secrets/tests/Tests.elm b/exercises/concept/secrets/tests/Tests.elm index e69de29b..d82f8d8d 100644 --- a/exercises/concept/secrets/tests/Tests.elm +++ b/exercises/concept/secrets/tests/Tests.elm @@ -0,0 +1,52 @@ +module Tests exposing (tests) + +import Expect +import Secrets exposing (..) +import Test exposing (..) + + +tests : Test +tests = + describe "Secrets" + [ describe "1" + [ test "Shift 8 right by 2" <| + \_ -> + shiftBack 2 8 + |> Expect.equal 2 + , test "Shift -8 right by 2" <| + \_ -> + shiftBack 2 -8 + |> Expect.equal 1073741822 + ] + , describe "2" + [ test "Set bits in 5" <| + \_ -> + setBits 3 5 + |> Expect.equal 7 + , test "Set bits in 5,652" <| + \_ -> + setBits 26150 5652 + |> Expect.equal 30262 + ] + , describe "3" + [ test "Flip bits in 5" <| + \_ -> + flipBits 11 5 + |> Expect.equal 14 + , test "Flip bits in 38460" <| + \_ -> + flipBits 15471 38460 + |> Expect.equal 43603 + + ] + , describe "4" + [ test "Clear bits from 5" <| + \_ -> + clearBits 11 5 + |> Expect.equal 4 + , test "Clear bits from 90" <| + \_ -> + clearBits 240 90 + |> Expect.equal 10 + ] + ] From 6e4d3f33cfee211dd0f0c6910735d458508ec914 Mon Sep 17 00:00:00 2001 From: Stewart Murrie Date: Tue, 5 Nov 2024 14:40:02 -0800 Subject: [PATCH 11/26] Removes the WIP status from the exercise --- config.json | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/config.json b/config.json index 657705ee..3be694a4 100644 --- a/config.json +++ b/config.json @@ -345,8 +345,7 @@ "name": "secrets", "uuid": "89a899b1-8cb2-4099-98c9-468723728a28", "concepts": ["bitwise-operations"], - "prerequisites": ["basics-1", "basics-2"], - "status": "wip" + "prerequisites": ["basics-1", "basics-2"] } ], "practice": [ From f8fca1a824ad078aa5a31a64b5681fbb2d0a923b Mon Sep 17 00:00:00 2001 From: Stewart Murrie Date: Tue, 5 Nov 2024 14:48:40 -0800 Subject: [PATCH 12/26] Fixes typo in the exercise instructions --- exercises/concept/secrets/.docs/instructions.md | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/exercises/concept/secrets/.docs/instructions.md b/exercises/concept/secrets/.docs/instructions.md index 17d12a9b..7694351b 100644 --- a/exercises/concept/secrets/.docs/instructions.md +++ b/exercises/concept/secrets/.docs/instructions.md @@ -2,7 +2,7 @@ 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 methods to help decrypt the message. +You will need to write the functions to help decrypt the message. ## 1. Shift back the bits @@ -24,7 +24,7 @@ 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. ```elm -setBits 66 212 --> 64 +setBits 66 212 --> 64 ``` ## 3. Flip specific bits @@ -51,4 +51,4 @@ All other bits should be kept unchanged. ```elm clearBits 2 15 --> 13 -``` \ No newline at end of file +``` From b073b12c013a2309e178017eb04c5701493f8d05 Mon Sep 17 00:00:00 2001 From: Stewart Murrie Date: Thu, 7 Nov 2024 11:25:15 -0800 Subject: [PATCH 13/26] Adds Secret handshake to bitwise exercises --- concepts/bitwise-operations/links.json | 2 +- config.json | 25 +++++++++++++++------ exercises/concept/secrets/.meta/config.json | 4 +++- 3 files changed, 22 insertions(+), 9 deletions(-) diff --git a/concepts/bitwise-operations/links.json b/concepts/bitwise-operations/links.json index eddbc1b5..f3c37daa 100644 --- a/concepts/bitwise-operations/links.json +++ b/concepts/bitwise-operations/links.json @@ -7,4 +7,4 @@ "url": "https://guide.elm-lang.org/appendix/types_as_bits", "description": "Documentation on how Elm represents types as bits" } -] \ No newline at end of file +] diff --git a/config.json b/config.json index 3be694a4..f1f4d154 100644 --- a/config.json +++ b/config.json @@ -342,10 +342,15 @@ }, { "slug": "secrets", - "name": "secrets", + "name": "Secrets", "uuid": "89a899b1-8cb2-4099-98c9-468723728a28", - "concepts": ["bitwise-operations"], - "prerequisites": ["basics-1", "basics-2"] + "concepts": [ + "bitwise-operations" + ], + "prerequisites": [ + "basics-1", + "basics-2" + ] } ], "practice": [ @@ -510,8 +515,12 @@ "slug": "allergies", "name": "Allergies", "uuid": "29b5a28a-417a-4cee-ba6f-9dd942ceffaa", - "practices": ["bitwise-operations"], - "prerequisites": ["bitwise-operations"], + "practices": [ + "bitwise-operations" + ], + "prerequisites": [ + "bitwise-operations" + ], "difficulty": 4, "topics": [ "enumerations", @@ -1261,8 +1270,11 @@ "slug": "secret-handshake", "name": "Secret Handshake", "uuid": "6c7a96ec-0bfb-4284-bd9f-2aa6989c3bb5", - "practices": [], + "practices": [ + "bitwise-operations" + ], "prerequisites": [ + "bitwise-operations", "custom-types", "pattern-matching", "maybe", @@ -1517,7 +1529,6 @@ "slug": "bitwise-operations", "name": "Bitwise Operations" } - ], "key_features": [ { diff --git a/exercises/concept/secrets/.meta/config.json b/exercises/concept/secrets/.meta/config.json index be850170..42e03920 100644 --- a/exercises/concept/secrets/.meta/config.json +++ b/exercises/concept/secrets/.meta/config.json @@ -1,5 +1,7 @@ { - "authors": ["stewartmurrie"], + "authors": [ + "stewartmurrie" + ], "files": { "solution": [ "src/Secrets.elm" From 54545de99f37942d2cc834eb0bff89f389126747 Mon Sep 17 00:00:00 2001 From: Stewart Murrie Date: Thu, 7 Nov 2024 15:54:36 -0800 Subject: [PATCH 14/26] Updates heading --- concepts/bitwise-operations/introduction.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/concepts/bitwise-operations/introduction.md b/concepts/bitwise-operations/introduction.md index 546e89d4..be10a291 100644 --- a/concepts/bitwise-operations/introduction.md +++ b/concepts/bitwise-operations/introduction.md @@ -1,4 +1,4 @@ -# About Bitwise Operations +# Introduction Bitwise operations allow us to manipulate individual digits within binary numbers. These operations are fundamental in computing, providing an efficient way to perform low-level tasks, such as controlling specific bits in memory, optimizing mathematical calculations, encryption, communications, and encoding/decoding data. From b0cf475f6636d2777b3fc9c3531fee9e49d62e08 Mon Sep 17 00:00:00 2001 From: Stewart Murrie Date: Thu, 7 Nov 2024 15:55:02 -0800 Subject: [PATCH 15/26] Updates for consistency & formatting --- exercises/concept/secrets/.meta/Exemplar.elm | 8 +++---- exercises/concept/secrets/src/Secrets.elm | 2 +- exercises/concept/secrets/tests/Tests.elm | 23 ++++++++++---------- 3 files changed, 16 insertions(+), 17 deletions(-) diff --git a/exercises/concept/secrets/.meta/Exemplar.elm b/exercises/concept/secrets/.meta/Exemplar.elm index d38166c6..cdc0a300 100644 --- a/exercises/concept/secrets/.meta/Exemplar.elm +++ b/exercises/concept/secrets/.meta/Exemplar.elm @@ -1,6 +1,6 @@ -module Secrets exposing (..) +module Secrets exposing (clearBits, flipBits, setBits, shiftBack) -import Bitwise exposing (..) +import Bitwise shiftBack amount value = @@ -17,5 +17,5 @@ flipBits mask value = clearBits mask value = mask - |> Bitwise.complement - |> Bitwise.and value + |> Bitwise.complement + |> Bitwise.and value diff --git a/exercises/concept/secrets/src/Secrets.elm b/exercises/concept/secrets/src/Secrets.elm index 469179b7..4d000a47 100644 --- a/exercises/concept/secrets/src/Secrets.elm +++ b/exercises/concept/secrets/src/Secrets.elm @@ -1,4 +1,4 @@ -module Secrets exposing (..) +module Secrets exposing (clearBits, flipBits, setBits, shiftBack) shiftBack amount value = diff --git a/exercises/concept/secrets/tests/Tests.elm b/exercises/concept/secrets/tests/Tests.elm index d82f8d8d..8ef8510e 100644 --- a/exercises/concept/secrets/tests/Tests.elm +++ b/exercises/concept/secrets/tests/Tests.elm @@ -1,8 +1,8 @@ module Tests exposing (tests) import Expect -import Secrets exposing (..) -import Test exposing (..) +import Secrets +import Test exposing (Test, describe, test) tests : Test @@ -11,42 +11,41 @@ tests = [ describe "1" [ test "Shift 8 right by 2" <| \_ -> - shiftBack 2 8 - |> Expect.equal 2 + Secrets.shiftBack 2 8 + |> Expect.equal 2 , test "Shift -8 right by 2" <| \_ -> - shiftBack 2 -8 + Secrets.shiftBack 2 -8 |> Expect.equal 1073741822 ] , describe "2" [ test "Set bits in 5" <| \_ -> - setBits 3 5 + Secrets.setBits 3 5 |> Expect.equal 7 , test "Set bits in 5,652" <| \_ -> - setBits 26150 5652 + Secrets.setBits 26150 5652 |> Expect.equal 30262 ] , describe "3" [ test "Flip bits in 5" <| \_ -> - flipBits 11 5 + Secrets.flipBits 11 5 |> Expect.equal 14 , test "Flip bits in 38460" <| \_ -> - flipBits 15471 38460 + Secrets.flipBits 15471 38460 |> Expect.equal 43603 - ] , describe "4" [ test "Clear bits from 5" <| \_ -> - clearBits 11 5 + Secrets.clearBits 11 5 |> Expect.equal 4 , test "Clear bits from 90" <| \_ -> - clearBits 240 90 + Secrets.clearBits 240 90 |> Expect.equal 10 ] ] From 7d00134914abbb9efbb294c004023325a39bfd97 Mon Sep 17 00:00:00 2001 From: Stewart Murrie Date: Thu, 7 Nov 2024 15:56:55 -0800 Subject: [PATCH 16/26] Update for consistency with other exercises --- exercises/concept/secrets/elm.json | 11 +++++++---- 1 file changed, 7 insertions(+), 4 deletions(-) diff --git a/exercises/concept/secrets/elm.json b/exercises/concept/secrets/elm.json index 38691915..22c9e137 100644 --- a/exercises/concept/secrets/elm.json +++ b/exercises/concept/secrets/elm.json @@ -6,11 +6,14 @@ "elm-version": "0.19.1", "dependencies": { "direct": { - "elm/browser": "1.0.2", - "elm/core": "1.0.5" + "elm/core": "1.0.5", + "elm/json": "1.1.3", + "elm/parser": "1.1.0", + "elm/random": "1.0.0", + "elm/regex": "1.0.0", + "elm/time": "1.0.0" }, - "indirect": { - } + "indirect": {} }, "test-dependencies": { "direct": { From aa79d27c941cb0e694b449521544699e034a9f6e Mon Sep 17 00:00:00 2001 From: Stewart Murrie Date: Sat, 16 Nov 2024 17:27:49 -0800 Subject: [PATCH 17/26] Attempts to use templates --- concepts/bitwise-operations/introduction.md | 141 ------------------ .../bitwise-operations/introduction.md.tpl | 3 + .../concept/secrets/.docs/introduction.md | 139 ----------------- .../concept/secrets/.docs/introduction.md.tpl | 3 + 4 files changed, 6 insertions(+), 280 deletions(-) create mode 100644 concepts/bitwise-operations/introduction.md.tpl create mode 100644 exercises/concept/secrets/.docs/introduction.md.tpl diff --git a/concepts/bitwise-operations/introduction.md b/concepts/bitwise-operations/introduction.md index be10a291..e10b99d0 100644 --- a/concepts/bitwise-operations/introduction.md +++ b/concepts/bitwise-operations/introduction.md @@ -1,142 +1 @@ # Introduction - -Bitwise operations allow us to manipulate individual digits within binary numbers. These operations are fundamental in computing, providing an efficient way to perform low-level tasks, such as controlling specific bits in memory, optimizing mathematical calculations, encryption, communications, and encoding/decoding data. - -## Understanding 32-bit Integers in Elm - -In Elm, integers are stored as 32-bit signed integers using [two's complement](https://en.wikipedia.org/wiki/Two%27s_complement) representation, meaning they use 32 binary digits (_bits_) to store each integer value. This bit limit affects the range of integers that Elm can represent directly: - -Positive Range: 0 to 2^31 - 1 (or 0 to 2,147,483,647) -Negative Range: -2^31 to -1 (or -2,147,483,648 to -1) - -For example, the integer `5` is represented in binary as `00000000000000000000000000000101`, although usually we ignore the leading zeros and just say `101`. - -## Bitwise operations - -Elm provides several bitwise operators in its [Bitwise module](https://package.elm-lang.org/packages/elm/core/latest/Bitwise) - -## Basic operations - -Modifying individual bits of a number is called _masking_. A _mask_ is a number where specific bits have been set in a particular way to manipulate another number using bitwise operators such as `and`, `or`, and `xor`. - -### and - -`and` combines two numbers by keeping only the bits that are `1` in both. For example: - -```elm -Bitwise.and 21 13 --> 5 --- 21 = 10101 --- 13 = 01101 --- and = 00101 = 5 -``` - -This is useful for checking to see if an individual bit is set. For example, to check if the 4th bit of a number is set to `1`, `and` it with a mask of `01000` (`8` in decimal) and see if the result is non-zero: - -```elm -Bitwise.and 13 8 --> 8 --- 13 = 01101 --- 8 = 01000 --- and = 01000 = 8 - -Bitwise.and 21 8 -> 0 --- 21 = 10101 --- 8 = 01000 --- and = 00000 = 0 -``` - -### or - -`or` combines two numbers by setting each bit to `1` if it is `1` in either or both numbers. - -```elm -Bitwise.or 21 13 --> 29 --- 21 = 10101 --- 13 = 01101 --- or = 11101 = 29 -``` - -This is useful for setting a specific bit to `1`. For example, to set the 2nd bit in `10101`, `or` it with the mask `00010`: - -```elm -Bitwise.or 21 2 --> 23 --- 21 = 10101 --- 2 = 00010 --- or = 10111 = 23 -``` - -### Exclusive-or (xor) - -`xor` combines two numbers by setting each bit to `1` if it is `1` in one number but `0` in the other. - -```elm -Bitwise.xor 21 13 --> 24 --- 21 = 10101 --- 13 = 01101 --- xor = 11000 = 24 -``` - -This is useful for flipping a bit to its opposite value: - -```elm -Bitwise.xor 21 4 --> 17 --- 21 = 10101 --- 4 = 00100 --- xor = 10001 = 17 - -Bitwise.xor 17 4 --> 21 --- 17 = 10001 --- 4 = 00100 --- xor = 10101 = 21 -``` - -### Complement - -`complement` inverts each bit of a number (`0` becomes `1`, `1` becomes `0`). - -Note that this will result in positive numbers becoming negative, and negative numbers becoming positive. This is because negative numbers in binary are represented with `1` in the left-most position. - -```elm -Bitwise.complement 21 --> -22 --- 21 = 00000000000000000000000000010101 --- complement = 11111111111111111111111111101010 = -22 -``` - -## Bit shifting - -The following operators move bits left or right by a specified number of positions, effectively multiplying or dividing by powers of 2. - -`shiftLeftBy` moves bits to the left, filling in with `0` from the right-hand side. For example, to shift `21` left by 3 places: - -```elm -Bitwise.shiftLeftBy 3 21 --> 168 --- 21 = 10101 --- shiftLeftBy 3 = 10101000 = 168 -``` - -This is the same as saying `21 * 2^3 = 21 * 2 * 2 * 2 = 168` - -`shiftRightBy`: Moves bits to the right: - -```elm -Bitwise.shiftRightBy 2 21 --> 5 --- 21 = 10101 --- shiftRightBy 2 = 00101 = 5 -``` - -Shifiting to the right by 2 places is the same as integer division by 4. - -Note that this function duplicates whatever value is in the leftmost bit. So negative numbers will stay negative: - -```elm -Bitwise.shiftRightBy 3 -21 --> -6 --- -21 = 111...10101 --- shiftRightBy 3 = 111...11101 = -6 -``` - -If you want to shift right and fill in with zeros, use `shiftRightZfBy`: - -```elm -Bitwise.shiftRightZfBy 3 -21 --> 1073741818 --- -21 = 111...10101 --- shiftRightBy 3 = 00111...11101 = 1073741818 -``` diff --git a/concepts/bitwise-operations/introduction.md.tpl b/concepts/bitwise-operations/introduction.md.tpl new file mode 100644 index 00000000..41cf343b --- /dev/null +++ b/concepts/bitwise-operations/introduction.md.tpl @@ -0,0 +1,3 @@ +# Introduction + +%{concept: bitwise-operations} diff --git a/exercises/concept/secrets/.docs/introduction.md b/exercises/concept/secrets/.docs/introduction.md index 546e89d4..383a7bb1 100644 --- a/exercises/concept/secrets/.docs/introduction.md +++ b/exercises/concept/secrets/.docs/introduction.md @@ -1,142 +1,3 @@ # About Bitwise Operations -Bitwise operations allow us to manipulate individual digits within binary numbers. These operations are fundamental in computing, providing an efficient way to perform low-level tasks, such as controlling specific bits in memory, optimizing mathematical calculations, encryption, communications, and encoding/decoding data. -## Understanding 32-bit Integers in Elm - -In Elm, integers are stored as 32-bit signed integers using [two's complement](https://en.wikipedia.org/wiki/Two%27s_complement) representation, meaning they use 32 binary digits (_bits_) to store each integer value. This bit limit affects the range of integers that Elm can represent directly: - -Positive Range: 0 to 2^31 - 1 (or 0 to 2,147,483,647) -Negative Range: -2^31 to -1 (or -2,147,483,648 to -1) - -For example, the integer `5` is represented in binary as `00000000000000000000000000000101`, although usually we ignore the leading zeros and just say `101`. - -## Bitwise operations - -Elm provides several bitwise operators in its [Bitwise module](https://package.elm-lang.org/packages/elm/core/latest/Bitwise) - -## Basic operations - -Modifying individual bits of a number is called _masking_. A _mask_ is a number where specific bits have been set in a particular way to manipulate another number using bitwise operators such as `and`, `or`, and `xor`. - -### and - -`and` combines two numbers by keeping only the bits that are `1` in both. For example: - -```elm -Bitwise.and 21 13 --> 5 --- 21 = 10101 --- 13 = 01101 --- and = 00101 = 5 -``` - -This is useful for checking to see if an individual bit is set. For example, to check if the 4th bit of a number is set to `1`, `and` it with a mask of `01000` (`8` in decimal) and see if the result is non-zero: - -```elm -Bitwise.and 13 8 --> 8 --- 13 = 01101 --- 8 = 01000 --- and = 01000 = 8 - -Bitwise.and 21 8 -> 0 --- 21 = 10101 --- 8 = 01000 --- and = 00000 = 0 -``` - -### or - -`or` combines two numbers by setting each bit to `1` if it is `1` in either or both numbers. - -```elm -Bitwise.or 21 13 --> 29 --- 21 = 10101 --- 13 = 01101 --- or = 11101 = 29 -``` - -This is useful for setting a specific bit to `1`. For example, to set the 2nd bit in `10101`, `or` it with the mask `00010`: - -```elm -Bitwise.or 21 2 --> 23 --- 21 = 10101 --- 2 = 00010 --- or = 10111 = 23 -``` - -### Exclusive-or (xor) - -`xor` combines two numbers by setting each bit to `1` if it is `1` in one number but `0` in the other. - -```elm -Bitwise.xor 21 13 --> 24 --- 21 = 10101 --- 13 = 01101 --- xor = 11000 = 24 -``` - -This is useful for flipping a bit to its opposite value: - -```elm -Bitwise.xor 21 4 --> 17 --- 21 = 10101 --- 4 = 00100 --- xor = 10001 = 17 - -Bitwise.xor 17 4 --> 21 --- 17 = 10001 --- 4 = 00100 --- xor = 10101 = 21 -``` - -### Complement - -`complement` inverts each bit of a number (`0` becomes `1`, `1` becomes `0`). - -Note that this will result in positive numbers becoming negative, and negative numbers becoming positive. This is because negative numbers in binary are represented with `1` in the left-most position. - -```elm -Bitwise.complement 21 --> -22 --- 21 = 00000000000000000000000000010101 --- complement = 11111111111111111111111111101010 = -22 -``` - -## Bit shifting - -The following operators move bits left or right by a specified number of positions, effectively multiplying or dividing by powers of 2. - -`shiftLeftBy` moves bits to the left, filling in with `0` from the right-hand side. For example, to shift `21` left by 3 places: - -```elm -Bitwise.shiftLeftBy 3 21 --> 168 --- 21 = 10101 --- shiftLeftBy 3 = 10101000 = 168 -``` - -This is the same as saying `21 * 2^3 = 21 * 2 * 2 * 2 = 168` - -`shiftRightBy`: Moves bits to the right: - -```elm -Bitwise.shiftRightBy 2 21 --> 5 --- 21 = 10101 --- shiftRightBy 2 = 00101 = 5 -``` - -Shifiting to the right by 2 places is the same as integer division by 4. - -Note that this function duplicates whatever value is in the leftmost bit. So negative numbers will stay negative: - -```elm -Bitwise.shiftRightBy 3 -21 --> -6 --- -21 = 111...10101 --- shiftRightBy 3 = 111...11101 = -6 -``` - -If you want to shift right and fill in with zeros, use `shiftRightZfBy`: - -```elm -Bitwise.shiftRightZfBy 3 -21 --> 1073741818 --- -21 = 111...10101 --- shiftRightBy 3 = 00111...11101 = 1073741818 -``` diff --git a/exercises/concept/secrets/.docs/introduction.md.tpl b/exercises/concept/secrets/.docs/introduction.md.tpl new file mode 100644 index 00000000..37bf4785 --- /dev/null +++ b/exercises/concept/secrets/.docs/introduction.md.tpl @@ -0,0 +1,3 @@ +# About Bitwise Operations + +%{concept: bitwise-operations} From ce39d308bdbd6ac7f231b30be60f410641d826ef Mon Sep 17 00:00:00 2001 From: Stewart Murrie Date: Sat, 16 Nov 2024 17:40:00 -0800 Subject: [PATCH 18/26] Updates to the templated --- concepts/bitwise-operations/about.md | 143 +----------------- concepts/bitwise-operations/introduction.md | 141 +++++++++++++++++ .../bitwise-operations/introduction.md.tpl | 3 - .../concept/secrets/.docs/introduction.md | 3 - .../concept/secrets/.docs/introduction.md.tpl | 2 - 5 files changed, 142 insertions(+), 150 deletions(-) delete mode 100644 concepts/bitwise-operations/introduction.md.tpl delete mode 100644 exercises/concept/secrets/.docs/introduction.md diff --git a/concepts/bitwise-operations/about.md b/concepts/bitwise-operations/about.md index c0191450..6ecc8818 100644 --- a/concepts/bitwise-operations/about.md +++ b/concepts/bitwise-operations/about.md @@ -1,142 +1 @@ -# Bitwise Operations - -Bitwise operations allow us to manipulate individual digits within binary numbers. These operations are fundamental in computing, providing an efficient way to perform low-level tasks, such as controlling specific bits in memory, optimizing mathematical calculations, encryption, communications, and encoding/decoding data. - -## Understanding 32-bit Integers in Elm - -In Elm, integers are stored as 32-bit signed integers using [two's complement](https://en.wikipedia.org/wiki/Two%27s_complement) representation, meaning they use 32 binary digits (_bits_) to store each integer value. This bit limit affects the range of integers that Elm can represent directly: - -Positive Range: 0 to 2^31 - 1 (or 0 to 2,147,483,647) -Negative Range: -2^31 to -1 (or -2,147,483,648 to -1) - -For example, the integer `5` is represented in binary as `00000000000000000000000000000101`, although usually we ignore the leading zeros and just say `101`. - -## Bitwise operations - -Elm provides several bitwise operators in its [Bitwise module](https://package.elm-lang.org/packages/elm/core/latest/Bitwise) - -## Basic operations - -Modifying individual bits of a number is called _masking_. A _mask_ is a number where specific bits have been set in a particular way to manipulate another number using bitwise operators such as `and`, `or`, and `xor`. - -### and - -`and` combines two numbers by keeping only the bits that are `1` in both. For example: - -```elm -Bitwise.and 21 13 --> 5 --- 21 = 10101 --- 13 = 01101 --- and = 00101 = 5 -``` - -This is useful for checking to see if an individual bit is set. For example, to check if the 4th bit of a number is set to `1`, `and` it with a mask of `01000` (`8` in decimal) and see if the result is non-zero: - -```elm -Bitwise.and 13 8 --> 8 --- 13 = 01101 --- 8 = 01000 --- and = 01000 = 8 - -Bitwise.and 21 8 -> 0 --- 21 = 10101 --- 8 = 01000 --- and = 00000 = 0 -``` - -### or - -`or` combines two numbers by setting each bit to `1` if it is `1` in either or both numbers. - -```elm -Bitwise.or 21 13 --> 29 --- 21 = 10101 --- 13 = 01101 --- or = 11101 = 29 -``` - -This is useful for setting a specific bit to `1`. For example, to set the 2nd bit in `10101`, `or` it with the mask `00010`: - -```elm -Bitwise.or 21 2 --> 23 --- 21 = 10101 --- 2 = 00010 --- or = 10111 = 23 -``` - -### Exclusive-or (xor) - -`xor` combines two numbers by setting each bit to `1` if it is `1` in one number but `0` in the other. - -```elm -Bitwise.xor 21 13 --> 24 --- 21 = 10101 --- 13 = 01101 --- xor = 11000 = 24 -``` - -This is useful for flipping a bit to its opposite value: - -```elm -Bitwise.xor 21 4 --> 17 --- 21 = 10101 --- 4 = 00100 --- xor = 10001 = 17 - -Bitwise.xor 17 4 --> 21 --- 17 = 10001 --- 4 = 00100 --- xor = 10101 = 21 -``` - -### Complement - -`complement` inverts each bit of a number (`0` becomes `1`, `1` becomes `0`). - -Note that this will result in positive numbers becoming negative, and negative numbers becoming positive. This is because negative numbers in binary are represented with `1` in the left-most position. - -```elm -Bitwise.complement 21 --> -22 --- 21 = 00000000000000000000000000010101 --- complement = 11111111111111111111111111101010 = -22 -``` - -## Bit shifting - -The following operators move bits left or right by a specified number of positions, effectively multiplying or dividing by powers of 2. - -`shiftLeftBy` moves bits to the left, filling in with `0` from the right-hand side. For example, to shift `21` left by 3 places: - -```elm -Bitwise.shiftLeftBy 3 21 --> 168 --- 21 = 10101 --- shiftLeftBy 3 = 10101000 = 168 -``` - -This is the same as saying `21 * 2^3 = 21 * 2 * 2 * 2 = 168` - -`shiftRightBy`: Moves bits to the right: - -```elm -Bitwise.shiftRightBy 2 21 --> 5 --- 21 = 10101 --- shiftRightBy 2 = 00101 = 5 -``` - -Shifiting to the right by 2 places is the same as integer division by 4. - -Note that this function duplicates whatever value is in the leftmost bit. So negative numbers will stay negative: - -```elm -Bitwise.shiftRightBy 3 -21 --> -6 --- -21 = 111...10101 --- shiftRightBy 3 = 111...11101 = -6 -``` - -If you want to shift right and fill in with zeros, use `shiftRightZfBy`: - -```elm -Bitwise.shiftRightZfBy 3 -21 --> 1073741818 --- -21 = 111...10101 --- shiftRightBy 3 = 00111...11101 = 1073741818 -``` +(Will be a copy of `introduction.md` once that is approved) diff --git a/concepts/bitwise-operations/introduction.md b/concepts/bitwise-operations/introduction.md index e10b99d0..be10a291 100644 --- a/concepts/bitwise-operations/introduction.md +++ b/concepts/bitwise-operations/introduction.md @@ -1 +1,142 @@ # Introduction + +Bitwise operations allow us to manipulate individual digits within binary numbers. These operations are fundamental in computing, providing an efficient way to perform low-level tasks, such as controlling specific bits in memory, optimizing mathematical calculations, encryption, communications, and encoding/decoding data. + +## Understanding 32-bit Integers in Elm + +In Elm, integers are stored as 32-bit signed integers using [two's complement](https://en.wikipedia.org/wiki/Two%27s_complement) representation, meaning they use 32 binary digits (_bits_) to store each integer value. This bit limit affects the range of integers that Elm can represent directly: + +Positive Range: 0 to 2^31 - 1 (or 0 to 2,147,483,647) +Negative Range: -2^31 to -1 (or -2,147,483,648 to -1) + +For example, the integer `5` is represented in binary as `00000000000000000000000000000101`, although usually we ignore the leading zeros and just say `101`. + +## Bitwise operations + +Elm provides several bitwise operators in its [Bitwise module](https://package.elm-lang.org/packages/elm/core/latest/Bitwise) + +## Basic operations + +Modifying individual bits of a number is called _masking_. A _mask_ is a number where specific bits have been set in a particular way to manipulate another number using bitwise operators such as `and`, `or`, and `xor`. + +### and + +`and` combines two numbers by keeping only the bits that are `1` in both. For example: + +```elm +Bitwise.and 21 13 --> 5 +-- 21 = 10101 +-- 13 = 01101 +-- and = 00101 = 5 +``` + +This is useful for checking to see if an individual bit is set. For example, to check if the 4th bit of a number is set to `1`, `and` it with a mask of `01000` (`8` in decimal) and see if the result is non-zero: + +```elm +Bitwise.and 13 8 --> 8 +-- 13 = 01101 +-- 8 = 01000 +-- and = 01000 = 8 + +Bitwise.and 21 8 -> 0 +-- 21 = 10101 +-- 8 = 01000 +-- and = 00000 = 0 +``` + +### or + +`or` combines two numbers by setting each bit to `1` if it is `1` in either or both numbers. + +```elm +Bitwise.or 21 13 --> 29 +-- 21 = 10101 +-- 13 = 01101 +-- or = 11101 = 29 +``` + +This is useful for setting a specific bit to `1`. For example, to set the 2nd bit in `10101`, `or` it with the mask `00010`: + +```elm +Bitwise.or 21 2 --> 23 +-- 21 = 10101 +-- 2 = 00010 +-- or = 10111 = 23 +``` + +### Exclusive-or (xor) + +`xor` combines two numbers by setting each bit to `1` if it is `1` in one number but `0` in the other. + +```elm +Bitwise.xor 21 13 --> 24 +-- 21 = 10101 +-- 13 = 01101 +-- xor = 11000 = 24 +``` + +This is useful for flipping a bit to its opposite value: + +```elm +Bitwise.xor 21 4 --> 17 +-- 21 = 10101 +-- 4 = 00100 +-- xor = 10001 = 17 + +Bitwise.xor 17 4 --> 21 +-- 17 = 10001 +-- 4 = 00100 +-- xor = 10101 = 21 +``` + +### Complement + +`complement` inverts each bit of a number (`0` becomes `1`, `1` becomes `0`). + +Note that this will result in positive numbers becoming negative, and negative numbers becoming positive. This is because negative numbers in binary are represented with `1` in the left-most position. + +```elm +Bitwise.complement 21 --> -22 +-- 21 = 00000000000000000000000000010101 +-- complement = 11111111111111111111111111101010 = -22 +``` + +## Bit shifting + +The following operators move bits left or right by a specified number of positions, effectively multiplying or dividing by powers of 2. + +`shiftLeftBy` moves bits to the left, filling in with `0` from the right-hand side. For example, to shift `21` left by 3 places: + +```elm +Bitwise.shiftLeftBy 3 21 --> 168 +-- 21 = 10101 +-- shiftLeftBy 3 = 10101000 = 168 +``` + +This is the same as saying `21 * 2^3 = 21 * 2 * 2 * 2 = 168` + +`shiftRightBy`: Moves bits to the right: + +```elm +Bitwise.shiftRightBy 2 21 --> 5 +-- 21 = 10101 +-- shiftRightBy 2 = 00101 = 5 +``` + +Shifiting to the right by 2 places is the same as integer division by 4. + +Note that this function duplicates whatever value is in the leftmost bit. So negative numbers will stay negative: + +```elm +Bitwise.shiftRightBy 3 -21 --> -6 +-- -21 = 111...10101 +-- shiftRightBy 3 = 111...11101 = -6 +``` + +If you want to shift right and fill in with zeros, use `shiftRightZfBy`: + +```elm +Bitwise.shiftRightZfBy 3 -21 --> 1073741818 +-- -21 = 111...10101 +-- shiftRightBy 3 = 00111...11101 = 1073741818 +``` diff --git a/concepts/bitwise-operations/introduction.md.tpl b/concepts/bitwise-operations/introduction.md.tpl deleted file mode 100644 index 41cf343b..00000000 --- a/concepts/bitwise-operations/introduction.md.tpl +++ /dev/null @@ -1,3 +0,0 @@ -# Introduction - -%{concept: bitwise-operations} diff --git a/exercises/concept/secrets/.docs/introduction.md b/exercises/concept/secrets/.docs/introduction.md deleted file mode 100644 index 383a7bb1..00000000 --- a/exercises/concept/secrets/.docs/introduction.md +++ /dev/null @@ -1,3 +0,0 @@ -# About Bitwise Operations - - diff --git a/exercises/concept/secrets/.docs/introduction.md.tpl b/exercises/concept/secrets/.docs/introduction.md.tpl index 37bf4785..c8a5c24c 100644 --- a/exercises/concept/secrets/.docs/introduction.md.tpl +++ b/exercises/concept/secrets/.docs/introduction.md.tpl @@ -1,3 +1 @@ -# About Bitwise Operations - %{concept: bitwise-operations} From fc09e07daa6605de6ed4388aa207bd5afd8f211b Mon Sep 17 00:00:00 2001 From: Stewart Murrie Date: Sat, 16 Nov 2024 17:55:36 -0800 Subject: [PATCH 19/26] Simplified the examples. Added spacing. --- concepts/bitwise-operations/introduction.md | 77 +++++++-------------- 1 file changed, 25 insertions(+), 52 deletions(-) diff --git a/concepts/bitwise-operations/introduction.md b/concepts/bitwise-operations/introduction.md index be10a291..419dade4 100644 --- a/concepts/bitwise-operations/introduction.md +++ b/concepts/bitwise-operations/introduction.md @@ -1,13 +1,15 @@ # Introduction -Bitwise operations allow us to manipulate individual digits within binary numbers. These operations are fundamental in computing, providing an efficient way to perform low-level tasks, such as controlling specific bits in memory, optimizing mathematical calculations, encryption, communications, and encoding/decoding data. +Bitwise operations allow us to manipulate individual digits within binary numbers. +These operations are fundamental in computing, providing an efficient way to perform low-level tasks, such as controlling specific bits in memory, optimizing mathematical calculations, encryption, communications, and encoding/decoding data. ## Understanding 32-bit Integers in Elm -In Elm, integers are stored as 32-bit signed integers using [two's complement](https://en.wikipedia.org/wiki/Two%27s_complement) representation, meaning they use 32 binary digits (_bits_) to store each integer value. This bit limit affects the range of integers that Elm can represent directly: +In Elm, integers are stored as 32-bit signed integers using [two's complement](https://en.wikipedia.org/wiki/Two%27s_complement) representation, meaning they use 32 binary digits (_bits_) to store each integer value. +This bit limit affects the range of integers that Elm can represent directly: -Positive Range: 0 to 2^31 - 1 (or 0 to 2,147,483,647) -Negative Range: -2^31 to -1 (or -2,147,483,648 to -1) +Positive Range: 0 to 231 - 1 (or 0 to 2,147,483,647) +Negative Range: -231 to -1 (or -2,147,483,648 to -1). For example, the integer `5` is represented in binary as `00000000000000000000000000000101`, although usually we ignore the leading zeros and just say `101`. @@ -17,95 +19,65 @@ Elm provides several bitwise operators in its [Bitwise module](https://package.e ## Basic operations -Modifying individual bits of a number is called _masking_. A _mask_ is a number where specific bits have been set in a particular way to manipulate another number using bitwise operators such as `and`, `or`, and `xor`. +Modifying individual bits of a number is called _masking_. +A _mask_ is a number where specific bits have been set in a particular way to manipulate another number using bitwise operators such as `and`, `or`, and `xor`. ### and -`and` combines two numbers by keeping only the bits that are `1` in both. For example: +`and` combines two numbers by keeping only the bits that are `1` in both. +This is useful for checking to see if an individual bit is set. +For example, to check if the 4th bit of a number is set to `1`, `and` it with a mask of `01000` (`8` in decimal) and see if the result is non-zero: -```elm -Bitwise.and 21 13 --> 5 --- 21 = 10101 --- 13 = 01101 --- and = 00101 = 5 -``` - -This is useful for checking to see if an individual bit is set. For example, to check if the 4th bit of a number is set to `1`, `and` it with a mask of `01000` (`8` in decimal) and see if the result is non-zero: - -```elm +````elm Bitwise.and 13 8 --> 8 -- 13 = 01101 -- 8 = 01000 -- and = 01000 = 8 -Bitwise.and 21 8 -> 0 --- 21 = 10101 --- 8 = 01000 --- and = 00000 = 0 -``` ### or `or` combines two numbers by setting each bit to `1` if it is `1` in either or both numbers. - -```elm -Bitwise.or 21 13 --> 29 --- 21 = 10101 --- 13 = 01101 --- or = 11101 = 29 -``` - -This is useful for setting a specific bit to `1`. For example, to set the 2nd bit in `10101`, `or` it with the mask `00010`: +This is useful for setting a specific bit to `1`. +For example, to set the 2nd bit in `10101`, `or` it with the mask `00010`: ```elm Bitwise.or 21 2 --> 23 -- 21 = 10101 -- 2 = 00010 -- or = 10111 = 23 -``` +```` ### Exclusive-or (xor) `xor` combines two numbers by setting each bit to `1` if it is `1` in one number but `0` in the other. - -```elm -Bitwise.xor 21 13 --> 24 --- 21 = 10101 --- 13 = 01101 --- xor = 11000 = 24 -``` - This is useful for flipping a bit to its opposite value: -```elm +````elm Bitwise.xor 21 4 --> 17 -- 21 = 10101 -- 4 = 00100 -- xor = 10001 = 17 -Bitwise.xor 17 4 --> 21 --- 17 = 10001 --- 4 = 00100 --- xor = 10101 = 21 -``` - ### Complement `complement` inverts each bit of a number (`0` becomes `1`, `1` becomes `0`). -Note that this will result in positive numbers becoming negative, and negative numbers becoming positive. This is because negative numbers in binary are represented with `1` in the left-most position. +Note that this will result in positive numbers becoming negative, and negative numbers becoming positive. +This is because negative numbers in binary are represented with `1` in the left-most position. ```elm Bitwise.complement 21 --> -22 -- 21 = 00000000000000000000000000010101 -- complement = 11111111111111111111111111101010 = -22 -``` +```` ## Bit shifting The following operators move bits left or right by a specified number of positions, effectively multiplying or dividing by powers of 2. -`shiftLeftBy` moves bits to the left, filling in with `0` from the right-hand side. For example, to shift `21` left by 3 places: +`shiftLeftBy` moves bits to the left, filling in with `0` from the right-hand side. +For example, to shift `21` left by 3 places: ```elm Bitwise.shiftLeftBy 3 21 --> 168 @@ -123,14 +95,15 @@ Bitwise.shiftRightBy 2 21 --> 5 -- shiftRightBy 2 = 00101 = 5 ``` -Shifiting to the right by 2 places is the same as integer division by 4. +Shifting to the right by 2 places is the same as integer division by 4. -Note that this function duplicates whatever value is in the leftmost bit. So negative numbers will stay negative: +Note that this function duplicates whatever value is in the leftmost bit. +So, negative numbers will stay negative: ```elm Bitwise.shiftRightBy 3 -21 --> -6 -- -21 = 111...10101 --- shiftRightBy 3 = 111...11101 = -6 +-- shiftRightZfBy 3 = 111...11101 = -6 ``` If you want to shift right and fill in with zeros, use `shiftRightZfBy`: From e76389a60ecbee1e04d9fddeef5c0d05e5d493fd Mon Sep 17 00:00:00 2001 From: Stewart Murrie Date: Sat, 16 Nov 2024 17:57:31 -0800 Subject: [PATCH 20/26] Added general hint linking to package docs --- exercises/concept/secrets/.docs/hints.md | 2 ++ 1 file changed, 2 insertions(+) diff --git a/exercises/concept/secrets/.docs/hints.md b/exercises/concept/secrets/.docs/hints.md index 76949931..e57342b7 100644 --- a/exercises/concept/secrets/.docs/hints.md +++ b/exercises/concept/secrets/.docs/hints.md @@ -1,5 +1,7 @@ # Hints +- The documentation for the `Bitwise` package can be found [here](https://package.elm-lang.org/packages/elm/core/latest/Bitwise) + ## 1. Shift back the bits - There are two functions for shifting bits to the right, but only one will always insert a 0. From 6ceb47fca4bef8579709b29f305bf8c0898583db Mon Sep 17 00:00:00 2001 From: Stewart Murrie Date: Sat, 16 Nov 2024 18:08:35 -0800 Subject: [PATCH 21/26] Adds helpful links to the hints --- exercises/concept/secrets/.docs/hints.md | 19 +++++++++++++------ 1 file changed, 13 insertions(+), 6 deletions(-) diff --git a/exercises/concept/secrets/.docs/hints.md b/exercises/concept/secrets/.docs/hints.md index e57342b7..e7002d97 100644 --- a/exercises/concept/secrets/.docs/hints.md +++ b/exercises/concept/secrets/.docs/hints.md @@ -1,20 +1,27 @@ # Hints -- The documentation for the `Bitwise` package can be found [here](https://package.elm-lang.org/packages/elm/core/latest/Bitwise) +- The documentation for the `Bitwise` package can be found [here][bitewise-docs] ## 1. Shift back the bits -- There are two functions for shifting bits to the right, but only one will always insert a 0. +- There are two functions for shifting bits to the right, but [only one][bitwise-shiftRightZfBy] will always insert a 0. ## 2. Set some bits -- One of the bitwise functions will always set a bit to 1 where the bits in both values are 1. +- [One of the bitwise functions][bitwise-or] will always set a bit to 1 where the bits in both values are 1. ## 3. Flip specific bits -- There is an bitwise function that will flip a bit where the mask is 1. +- There is [a bitwise function][bitwise-xor] that will flip a bit where the mask is 1. ## 4. Clear specific bits -- One of the bitwise functions clears bits where the bit in the mask is 0. -- But you may need to combine it with another function to clear bits where the mask is 1. +- [One of the bitwise functions][bitwise-and] clears bits where the bit in the mask is 0. +- But, you may need to combine it with [another function][bitwise-complement] to clear bits where the mask is 1. + +[bitwise-docs]: https://package.elm-lang.org/packages/elm/core/latest/Bitwise +[bitwise-shiftRightZfBy]: https://package.elm-lang.org/packages/elm/core/latest/Bitwise#shiftRightZfBy +[bitwise-or]: https://package.elm-lang.org/packages/elm/core/latest/Bitwise#or +[bitwise-xor]: https://package.elm-lang.org/packages/elm/core/latest/Bitwise#xor +[bitwise-complement]: https://package.elm-lang.org/packages/elm/core/latest/Bitwise#complement +[bitwise-and]: https://package.elm-lang.org/packages/elm/core/latest/Bitwise#and From 40ad002b6dfe46508dd7d8eda7520ce1bc84935d Mon Sep 17 00:00:00 2001 From: Stewart Murrie Date: Sat, 16 Nov 2024 18:10:11 -0800 Subject: [PATCH 22/26] Fixes typos in the hints --- exercises/concept/secrets/.docs/hints.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/exercises/concept/secrets/.docs/hints.md b/exercises/concept/secrets/.docs/hints.md index e7002d97..eb947ca5 100644 --- a/exercises/concept/secrets/.docs/hints.md +++ b/exercises/concept/secrets/.docs/hints.md @@ -1,6 +1,6 @@ # Hints -- The documentation for the `Bitwise` package can be found [here][bitewise-docs] +- The documentation for the `Bitwise` package can be found [here][bitwise-docs]. ## 1. Shift back the bits From 6a60cab269ba270bade42e15fc62820d9d824cdb Mon Sep 17 00:00:00 2001 From: Stewart Murrie Date: Sat, 16 Nov 2024 18:53:55 -0800 Subject: [PATCH 23/26] Adds 5th concept step to tie it all together --- exercises/concept/secrets/.docs/hints.md | 11 +++++++++++ exercises/concept/secrets/.docs/instructions.md | 16 +++++++++++++++- exercises/concept/secrets/.meta/Exemplar.elm | 10 +++++++++- exercises/concept/secrets/src/Secrets.elm | 6 +++++- exercises/concept/secrets/tests/Tests.elm | 10 ++++++++++ 5 files changed, 50 insertions(+), 3 deletions(-) diff --git a/exercises/concept/secrets/.docs/hints.md b/exercises/concept/secrets/.docs/hints.md index eb947ca5..1c38b6aa 100644 --- a/exercises/concept/secrets/.docs/hints.md +++ b/exercises/concept/secrets/.docs/hints.md @@ -19,6 +19,17 @@ - [One of the bitwise functions][bitwise-and] clears bits where the bit in the mask is 0. - But, you may need to combine it with [another function][bitwise-complement] to clear bits where the mask is 1. +## 5. Decrypt a message + +- Apply the other functions you wrote to the input in the following order, taking the output of one and using it as the input to the next one: + +1. `setBits` +2. `flipBits` +3. `shiftBack` +4. `clearBits` + +For step 4, you'll need to convert the binary number with the 1st and 5th bits set (10001) to decimal. + [bitwise-docs]: https://package.elm-lang.org/packages/elm/core/latest/Bitwise [bitwise-shiftRightZfBy]: https://package.elm-lang.org/packages/elm/core/latest/Bitwise#shiftRightZfBy [bitwise-or]: https://package.elm-lang.org/packages/elm/core/latest/Bitwise#or diff --git a/exercises/concept/secrets/.docs/instructions.md b/exercises/concept/secrets/.docs/instructions.md index 7694351b..52260f8f 100644 --- a/exercises/concept/secrets/.docs/instructions.md +++ b/exercises/concept/secrets/.docs/instructions.md @@ -43,7 +43,7 @@ flipBits 23 157 --> 138 ## 4. Clear specific bits -Lastly, there are also certain bits that always decrypt to 0. +There are also certain bits that always decrypt to 0. Implement the `clearBits` functions that takes a mask and a value. The bits in the `value` should be set to 0 where the bit in the mask is 1. @@ -52,3 +52,17 @@ All other bits should be kept unchanged. ```elm clearBits 2 15 --> 13 ``` + +## 5. Decrypt a message + +Now that you have all the functions you need, you can decode your friend's message. +Implement the `decrypt` function that performs the following operations: + +1. Set the bits from the year your friend was born (1996) +2. Flip the result with the year that you first met (2009) +3. Shift the bits back by the number of classes you take together (5) +4. Clear the first and fifth bit. + +```elm +decrypt 380182 --> +``` diff --git a/exercises/concept/secrets/.meta/Exemplar.elm b/exercises/concept/secrets/.meta/Exemplar.elm index cdc0a300..b9d830f1 100644 --- a/exercises/concept/secrets/.meta/Exemplar.elm +++ b/exercises/concept/secrets/.meta/Exemplar.elm @@ -1,4 +1,4 @@ -module Secrets exposing (clearBits, flipBits, setBits, shiftBack) +module Secrets exposing (clearBits, flipBits, setBits, shiftBack, decrypt) import Bitwise @@ -19,3 +19,11 @@ clearBits mask value = mask |> Bitwise.complement |> Bitwise.and value + + +decrypt secret = + secret + |> setBits 1996 + |> flipBits 2009 + |> shiftBack 5 + |> clearBits 17 \ No newline at end of file diff --git a/exercises/concept/secrets/src/Secrets.elm b/exercises/concept/secrets/src/Secrets.elm index 4d000a47..46f37243 100644 --- a/exercises/concept/secrets/src/Secrets.elm +++ b/exercises/concept/secrets/src/Secrets.elm @@ -1,4 +1,4 @@ -module Secrets exposing (clearBits, flipBits, setBits, shiftBack) +module Secrets exposing (clearBits, decrypt, flipBits, setBits, shiftBack) shiftBack amount value = @@ -15,3 +15,7 @@ flipBits mask value = clearBits mask value = Debug.todo "Please implement clearBits" + + +decrypt secret = + Debug.todo "Please implement decrypt" diff --git a/exercises/concept/secrets/tests/Tests.elm b/exercises/concept/secrets/tests/Tests.elm index 8ef8510e..5d5090fd 100644 --- a/exercises/concept/secrets/tests/Tests.elm +++ b/exercises/concept/secrets/tests/Tests.elm @@ -48,4 +48,14 @@ tests = Secrets.clearBits 240 90 |> Expect.equal 10 ] + , describe "5" + [ test "Decrypt 12345" <| + \_ -> + Secrets.decrypt 12345 + |> Expect.equal 384 + , test "Decrypt 123456789" <| + \_ -> + Secrets.decrypt 123456789 + |> Expect.equal 3857984 + ] ] From 662f38393dda7ea8c095cffca6572cfc0cec4fa3 Mon Sep 17 00:00:00 2001 From: Stewart Murrie Date: Sat, 16 Nov 2024 19:00:49 -0800 Subject: [PATCH 24/26] Formatting --- exercises/concept/secrets/.meta/Exemplar.elm | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/exercises/concept/secrets/.meta/Exemplar.elm b/exercises/concept/secrets/.meta/Exemplar.elm index b9d830f1..e20f47cb 100644 --- a/exercises/concept/secrets/.meta/Exemplar.elm +++ b/exercises/concept/secrets/.meta/Exemplar.elm @@ -1,4 +1,4 @@ -module Secrets exposing (clearBits, flipBits, setBits, shiftBack, decrypt) +module Secrets exposing (clearBits, decrypt, flipBits, setBits, shiftBack) import Bitwise @@ -21,9 +21,9 @@ clearBits mask value = |> Bitwise.and value -decrypt secret = +decrypt secret = secret - |> setBits 1996 - |> flipBits 2009 - |> shiftBack 5 - |> clearBits 17 \ No newline at end of file + |> setBits 1996 + |> flipBits 2009 + |> shiftBack 5 + |> clearBits 17 From d7cfa4b45074e5edabc59fb4b967491ef2acc938 Mon Sep 17 00:00:00 2001 From: Stewart Murrie Date: Sun, 24 Nov 2024 14:36:14 -0800 Subject: [PATCH 25/26] Fixes doc bugs Fixes doc bugs --- concepts/bitwise-operations/about.md | 118 +++++++++++++++++- concepts/bitwise-operations/introduction.md | 15 +-- .../concept/secrets/.docs/instructions.md | 6 +- .../concept/secrets/.docs/introduction.md | 104 +++++++++++++++ .../concept/secrets/.docs/introduction.md.tpl | 2 + 5 files changed, 228 insertions(+), 17 deletions(-) create mode 100644 exercises/concept/secrets/.docs/introduction.md diff --git a/concepts/bitwise-operations/about.md b/concepts/bitwise-operations/about.md index 6ecc8818..fb478c0d 100644 --- a/concepts/bitwise-operations/about.md +++ b/concepts/bitwise-operations/about.md @@ -1 +1,117 @@ -(Will be a copy of `introduction.md` once that is approved) +# About + +## Bitwise operations + +Bitwise operations allow us to manipulate individual digits within binary numbers. +These operations are fundamental in computing, providing an efficient way to perform low-level tasks, such as controlling specific bits in memory, optimizing mathematical calculations, encryption, communications, and encoding/decoding data. + +### Understanding 32-bit Integers in Elm + +In Elm, integers are stored as 32-bit signed integers using [two's complement](https://en.wikipedia.org/wiki/Two%27s_complement) representation, meaning they use 32 binary digits (_bits_) to store each integer value. +This bit limit affects the range of integers that Elm can represent directly: + +Positive Range: 0 to 231 - 1 (or 0 to 2,147,483,647) +Negative Range: -231 to -1 (or -2,147,483,648 to -1). + +For example, the integer `5` is represented in binary as `00000000000000000000000000000101`, although usually we ignore the leading zeros and just say `101`. + +### Bitwise operations + +Elm provides several bitwise operators in its [Bitwise module](https://package.elm-lang.org/packages/elm/core/latest/Bitwise) + +### Basic operations + +Modifying individual bits of a number is called _masking_. +A _mask_ is a number where specific bits have been set in a particular way to manipulate another number using bitwise operators such as `and`, `or`, and `xor`. + +#### and + +`and` combines two numbers by keeping only the bits that are `1` in both. +This is useful for checking to see if an individual bit is set. +For example, to check if the 4th bit of a number is set to `1`, `and` it with a mask of `01000` (`8` in decimal) and see if the result is non-zero: + +````elm +Bitwise.and 13 8 --> 8 +-- 13 = 01101 +-- 8 = 01000 +-- and = 01000 = 8 + + +### or + +`or` combines two numbers by setting each bit to `1` if it is `1` in either or both numbers. +This is useful for setting a specific bit to `1`. +For example, to set the 2nd bit in `10101`, `or` it with the mask `00010`: + +```elm +Bitwise.or 21 2 --> 23 +-- 21 = 10101 +-- 2 = 00010 +-- or = 10111 = 23 +```` + +### Exclusive-or (xor) + +`xor` combines two numbers by setting each bit to `1` if it is `1` in one number but `0` in the other. +This is useful for flipping a bit to its opposite value: + +````elm +Bitwise.xor 21 4 --> 17 +-- 21 = 10101 +-- 4 = 00100 +-- xor = 10001 = 17 + +#### Complement + +`complement` inverts each bit of a number (`0` becomes `1`, `1` becomes `0`). + +Note that this will result in positive numbers becoming negative, and negative numbers becoming positive. +This is because negative numbers in binary are represented with `1` in the left-most position. + +```elm +Bitwise.complement 21 --> -22 +-- 21 = 00000000000000000000000000010101 +-- complement = 11111111111111111111111111101010 = -22 +```` + +### Bit shifting + +The following operators move bits left or right by a specified number of positions, effectively multiplying or dividing by powers of 2. + +`shiftLeftBy` moves bits to the left, filling in with `0` from the right-hand side. +For example, to shift `21` left by 3 places: + +```elm +Bitwise.shiftLeftBy 3 21 --> 168 +-- 21 = 10101 +-- shiftLeftBy 3 = 10101000 = 168 +``` + +This is the same as saying `21 * 2^3 = 21 * 2 * 2 * 2 = 168` + +`shiftRightBy`: Moves bits to the right: + +```elm +Bitwise.shiftRightBy 2 21 --> 5 +-- 21 = 10101 +-- shiftRightBy 2 = 00101 = 5 +``` + +Shifting to the right by 2 places is the same as integer division by 4. + +Note that this function duplicates whatever value is in the leftmost bit. +So, negative numbers will stay negative: + +```elm +Bitwise.shiftRightBy 3 -21 --> -6 +-- -21 = 111...10101 +-- shiftRightBy 3 = 111...11101 = -6 +``` + +If you want to shift right and fill in with zeros, use `shiftRightZfBy`: + +```elm +Bitwise.shiftRightZfBy 3 -21 --> 1073741818 +-- -21 = 111...10101 +-- shiftRightZfBy 3 = 00111...11101 = 1073741818 +``` diff --git a/concepts/bitwise-operations/introduction.md b/concepts/bitwise-operations/introduction.md index 419dade4..9580c794 100644 --- a/concepts/bitwise-operations/introduction.md +++ b/concepts/bitwise-operations/introduction.md @@ -1,17 +1,6 @@ # Introduction Bitwise operations allow us to manipulate individual digits within binary numbers. -These operations are fundamental in computing, providing an efficient way to perform low-level tasks, such as controlling specific bits in memory, optimizing mathematical calculations, encryption, communications, and encoding/decoding data. - -## Understanding 32-bit Integers in Elm - -In Elm, integers are stored as 32-bit signed integers using [two's complement](https://en.wikipedia.org/wiki/Two%27s_complement) representation, meaning they use 32 binary digits (_bits_) to store each integer value. -This bit limit affects the range of integers that Elm can represent directly: - -Positive Range: 0 to 231 - 1 (or 0 to 2,147,483,647) -Negative Range: -231 to -1 (or -2,147,483,648 to -1). - -For example, the integer `5` is represented in binary as `00000000000000000000000000000101`, although usually we ignore the leading zeros and just say `101`. ## Bitwise operations @@ -103,7 +92,7 @@ So, negative numbers will stay negative: ```elm Bitwise.shiftRightBy 3 -21 --> -6 -- -21 = 111...10101 --- shiftRightZfBy 3 = 111...11101 = -6 +-- shiftRightBy 3 = 111...11101 = -6 ``` If you want to shift right and fill in with zeros, use `shiftRightZfBy`: @@ -111,5 +100,5 @@ If you want to shift right and fill in with zeros, use `shiftRightZfBy`: ```elm Bitwise.shiftRightZfBy 3 -21 --> 1073741818 -- -21 = 111...10101 --- shiftRightBy 3 = 00111...11101 = 1073741818 +-- shiftRightZfBy 3 = 00111...11101 = 1073741818 ``` diff --git a/exercises/concept/secrets/.docs/instructions.md b/exercises/concept/secrets/.docs/instructions.md index 52260f8f..c7f2d684 100644 --- a/exercises/concept/secrets/.docs/instructions.md +++ b/exercises/concept/secrets/.docs/instructions.md @@ -20,7 +20,7 @@ shiftBack 2 42 --> 10 Next, there are some bits that need to be set to `1`. Implement the `setBits` function that takes a mask and value 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`. +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. ```elm @@ -45,7 +45,7 @@ flipBits 23 157 --> 138 There are also certain bits that always decrypt to 0. -Implement the `clearBits` functions that takes a mask and a value. +Implement the `clearBits` function that takes a mask and a value. 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. @@ -64,5 +64,5 @@ Implement the `decrypt` function that performs the following operations: 4. Clear the first and fifth bit. ```elm -decrypt 380182 --> +decrypt 380182 --> 11840 ``` diff --git a/exercises/concept/secrets/.docs/introduction.md b/exercises/concept/secrets/.docs/introduction.md new file mode 100644 index 00000000..6fc4f9b3 --- /dev/null +++ b/exercises/concept/secrets/.docs/introduction.md @@ -0,0 +1,104 @@ +# Introduction + +Bitwise operations allow us to manipulate individual digits within binary numbers. + +## Bitwise operations + +Elm provides several bitwise operators in its [Bitwise module](https://package.elm-lang.org/packages/elm/core/latest/Bitwise) + +### Basic operations + +Modifying individual bits of a number is called _masking_. +A _mask_ is a number where specific bits have been set in a particular way to manipulate another number using bitwise operators such as `and`, `or`, and `xor`. + +#### and + +`and` combines two numbers by keeping only the bits that are `1` in both. +This is useful for checking to see if an individual bit is set. +For example, to check if the 4th bit of a number is set to `1`, `and` it with a mask of `01000` (`8` in decimal) and see if the result is non-zero: + +````elm +Bitwise.and 13 8 --> 8 +-- 13 = 01101 +-- 8 = 01000 +-- and = 01000 = 8 + + +### or + +`or` combines two numbers by setting each bit to `1` if it is `1` in either or both numbers. +This is useful for setting a specific bit to `1`. +For example, to set the 2nd bit in `10101`, `or` it with the mask `00010`: + +```elm +Bitwise.or 21 2 --> 23 +-- 21 = 10101 +-- 2 = 00010 +-- or = 10111 = 23 +```` + +### Exclusive-or (xor) + +`xor` combines two numbers by setting each bit to `1` if it is `1` in one number but `0` in the other. +This is useful for flipping a bit to its opposite value: + +````elm +Bitwise.xor 21 4 --> 17 +-- 21 = 10101 +-- 4 = 00100 +-- xor = 10001 = 17 + +#### Complement + +`complement` inverts each bit of a number (`0` becomes `1`, `1` becomes `0`). + +Note that this will result in positive numbers becoming negative, and negative numbers becoming positive. +This is because negative numbers in binary are represented with `1` in the left-most position. + +```elm +Bitwise.complement 21 --> -22 +-- 21 = 00000000000000000000000000010101 +-- complement = 11111111111111111111111111101010 = -22 +```` + +### Bit shifting + +The following operators move bits left or right by a specified number of positions, effectively multiplying or dividing by powers of 2. + +`shiftLeftBy` moves bits to the left, filling in with `0` from the right-hand side. +For example, to shift `21` left by 3 places: + +```elm +Bitwise.shiftLeftBy 3 21 --> 168 +-- 21 = 10101 +-- shiftLeftBy 3 = 10101000 = 168 +``` + +This is the same as saying `21 * 2^3 = 21 * 2 * 2 * 2 = 168` + +`shiftRightBy`: Moves bits to the right: + +```elm +Bitwise.shiftRightBy 2 21 --> 5 +-- 21 = 10101 +-- shiftRightBy 2 = 00101 = 5 +``` + +Shifting to the right by 2 places is the same as integer division by 4. + +Note that this function duplicates whatever value is in the leftmost bit. +So, negative numbers will stay negative: + +```elm +Bitwise.shiftRightBy 3 -21 --> -6 +-- -21 = 111...10101 +-- shiftRightBy 3 = 111...11101 = -6 +``` + +If you want to shift right and fill in with zeros, use `shiftRightZfBy`: + +```elm +Bitwise.shiftRightZfBy 3 -21 --> 1073741818 +-- -21 = 111...10101 +-- shiftRightZfBy 3 = 00111...11101 = 1073741818 +``` diff --git a/exercises/concept/secrets/.docs/introduction.md.tpl b/exercises/concept/secrets/.docs/introduction.md.tpl index c8a5c24c..41cf343b 100644 --- a/exercises/concept/secrets/.docs/introduction.md.tpl +++ b/exercises/concept/secrets/.docs/introduction.md.tpl @@ -1 +1,3 @@ +# Introduction + %{concept: bitwise-operations} From 0ca833508ed47e01cf14acb14a1a513778e0c77c Mon Sep 17 00:00:00 2001 From: Stewart Murrie Date: Sun, 24 Nov 2024 15:06:08 -0800 Subject: [PATCH 26/26] Add contribs --- concepts/bitwise-operations/.meta/config.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/concepts/bitwise-operations/.meta/config.json b/concepts/bitwise-operations/.meta/config.json index 20332b45..de93ebb4 100644 --- a/concepts/bitwise-operations/.meta/config.json +++ b/concepts/bitwise-operations/.meta/config.json @@ -2,6 +2,6 @@ "authors": [ "stewartmurrie" ], - "contributors": [ ], + "contributors": ["jiegillet"], "blurb": "Learn how to use integer bitwise operations in Elm" }