How to resolve the algorithm Set right-adjacent bits step by step in the Mathematica/Wolfram Language programming language

Published on 23 June 2024 04:55 PM

How to resolve the algorithm Set right-adjacent bits step by step in the Mathematica/Wolfram Language programming language

Table of Contents

Problem Statement

Given a left-to-right ordered collection of e bits, b, where 1 <= e <= 10000, and a zero or more integer n : (if those bits are present in b and therefore also preserving the width, e). Some examples: Task:

Let's start with the solution:

Step by Step solution about How to resolve the algorithm Set right-adjacent bits step by step in the Mathematica/Wolfram Language programming language

This Wolfram programming language code defines a function called ShowSetRightBits that takes a binary string b and an integer n as input and returns a grid with two rows. The first row shows the original binary string b, and the second row shows the modified binary string with the n rightmost bits set to 1.

Here's how the code works:

  1. The Module function is used to create a local scope for the variables poss and chars.

  2. The Characters[b] function is used to convert the binary string b into a list of characters.

  3. The Position[chars, "1"] function is used to find the positions of all the "1" characters in the list of characters.

  4. The Outer[Plus, poss, Range[n]] function is used to create a list of lists, where each inner list contains the positions of the n rightmost "1" characters in the list of characters.

  5. The Union[Flatten[Outer[Plus, poss, Range[n]]]] function is used to remove any duplicate positions from the list of lists.

  6. The ReplacePart[chars, (List /@ poss) -> "1"] function is used to replace the characters at the specified positions with "1" characters.

  7. The StringJoin function is used to join the modified list of characters into a single string.

  8. The Grid function is used to create a grid with two rows. The first row contains the original binary string b, and the second row contains the modified binary string.

  9. Finally, the {{"In :", b}, {"Out:", StringJoin[ReplacePart[chars, (List /@ poss) -> "1"]]}} expression is used to create a list of two lists, where the first list contains the text "In: " followed by the original binary string b, and the second list contains the text "Out: " followed by the modified binary string.

Here are some examples of how the ShowSetRightBits function can be used:

ShowSetRightBits["1000", 2]
(* Output: {{"In :", "1000"}, {"Out:", "1100"}} *)

In this example, the input binary string is "1000" and the number of rightmost bits to set to 1 is 2. The output is a grid with two rows. The first row shows the original binary string "1000", and the second row shows the modified binary string "1100", where the two rightmost bits have been set to 1.

ShowSetRightBits["0100", 2]
(* Output: {{"In :", "0100"}, {"Out:", "0111"}} *)

In this example, the input binary string is "0100" and the number of rightmost bits to set to 1 is 2. The output is a grid with two rows. The first row shows the original binary string "0100", and the second row shows the modified binary string "0111", where the two rightmost bits have been set to 1.

ShowSetRightBits["0010", 2]
(* Output: {{"In :", "0010"}, {"Out:", "0011"}} *)

In this example, the input binary string is "0010" and the number of rightmost bits to set to 1 is 2. The output is a grid with two rows. The first row shows the original binary string "0010", and the second row shows the modified binary string "0011", where the two rightmost bits have been set to 1.

ShowSetRightBits["0000", 2]
(* Output: {{"In :", "0000"}, {"Out:", "0011"}} *)

In this example, the input binary string is "0000" and the number of rightmost bits to set to 1 is 2. The output is a grid with two rows. The first row shows the original binary string "0000", and the second row shows the modified binary string "0011", where the two rightmost bits have been set to 1.

ShowSetRightBits["010000000000100000000010000000010000000100000010000010000100010010", 0]
(* Output: {{"In :", "010000000000100000000010000000010000000100000010000010000100010010"}, {"Out:", "010000000000100000000010000000010000000100000010000010000100010010"}} *)

In this example, the input binary string is a long sequence of 0s and 1s, and the number of rightmost bits to set to 1 is 0. The output is a grid with two rows. The first row shows the original binary string, and the second row shows the same binary string, since no bits were set to 1.

ShowSetRightBits["010000000000100000000010000000010000000100000010000010000100010010", 1]
(* Output: {{"In :", "010000000000100000000010000000010000000100000010000010000100010010"}, {"Out:", "010000000000100000000010000000010000000100000010000010000100011010"}} *)

In this example, the input binary string is a long sequence of 0s and 1s, and the number of rightmost bits to set to 1 is 1. The output is a grid with two rows. The first row shows the original binary string, and the second row shows the modified binary string, where the rightmost bit has been set to 1.

ShowSetRightBits["010000000000100000000010000000010000000100000010000010000100010010", 2]
(* Output: {{"In :", "010000000000100000000010000000010000000100000010000010000100010010"}, {"Out:", "010000000000100000000010000000010000000100000010000010000100011110"}} *)

In this example, the input binary string is a long sequence of 0s and 1s, and the number of rightmost bits to set to 1 is 2. The output is a grid with two rows. The first row shows the original binary string, and the second row shows the modified binary string, where the two rightmost bits have been set to 1.

ShowSetRightBits["010000000000100000000010000000010000000100000010000010000100010010", 3]
(* Output: {{"In :", "010000000000100000000010000000010000000100000010000010000100010010"}, {"Out:", "01000000000 
<div id="sourcecode"/>

## Source code in the wolfram programming language
{% raw %}
```wolfram  
ClearAll[ShowSetRightBits]
ShowSetRightBits[b_String,n_Integer]:=Module[{poss,chars},
chars=Characters[b];
poss=Position[chars,"1"];
poss=Union[Flatten[Outer[Plus,poss,Range[n]]]];
{{"In :",b},{"Out:",StringJoin[ReplacePart[chars,(List/@poss)->"1"]]}}//Grid
]
ShowSetRightBits["1000",2]
ShowSetRightBits["0100",2]
ShowSetRightBits["0010",2]
ShowSetRightBits["0000",2]
ShowSetRightBits["010000000000100000000010000000010000000100000010000010000100010010",0]
ShowSetRightBits["010000000000100000000010000000010000000100000010000010000100010010",1]
ShowSetRightBits["010000000000100000000010000000010000000100000010000010000100010010",2]
ShowSetRightBits["010000000000100000000010000000010000000100000010000010000100010010",3]


 

{% endraw %}


You may also check:How to resolve the algorithm Bitmap/Flood fill step by step in the Python programming language
You may also check:How to resolve the algorithm Start from a main routine step by step in the ZX Spectrum Basic programming language
You may also check:How to resolve the algorithm Euler's sum of powers conjecture step by step in the ALGOL W programming language
You may also check:How to resolve the algorithm Balanced ternary step by step in the PicoLisp programming language
You may also check:How to resolve the algorithm Sieve of Eratosthenes step by step in the Julia programming language