Task: Set right-adjacent bits




Go: package main

import (
    "fmt"
    "strings"
)

type test struct {
    bs string
    n  int
}

func setRightBits(bits []byte, e, n int) []byte {
    if e == 0 || n <= 0 {
        return bits
    }
    bits2 := make([]byte, len(bits))
    copy(bits2, bits)
    for i := 0; i < e-1; i++ {
        c := bits[i]
        if c == 1 {
            j := i + 1
            for j <= i+n && j < e {
                bits2[j] = 1
                j++
            }
        }
    }
    return bits2
}

func main() {
    b := "010000000000100000000010000000010000000100000010000010000100010010"
    tests := []test{
        test{"1000", 2}, test{"0100", 2}, test{"0010", 2}, test{"0000", 2},
        test{b, 0}, test{b, 1}, test{b, 2}, test{b, 3},
    }
    for _, test := range tests {
        bs := test.bs
        e := len(bs)
        n := test.n
        fmt.Println("n =", n, "\b; Width e =", e, "\b:")
        fmt.Println("    Input b:", bs)
        bits := []byte(bs)
        for i := 0; i < len(bits); i++ {
            bits[i] = bits[i] - '0'
        }
        bits = setRightBits(bits, e, n)
        var sb strings.Builder
        for i := 0; i < len(bits); i++ {
            sb.WriteByte(bits[i] + '0')
        }
        fmt.Println("    Result :", sb.String())
    }
}








Ada: with Ada.Text_IO;

procedure Set_Right_Bits is

   type Bit_Number is new Positive range 1 .. 10_000;
   type Bit        is new Boolean;
   type Bit_Collection is array (Bit_Number range <>) of Bit
     with Pack;

   function Right_Adjacent (B : Bit_Collection;
                            N : Natural) return Bit_Collection
   is
      Result : Bit_Collection := B;
      Mask   : Bit_Collection := B;
   begin
      for A in 1 .. N loop
         Mask   := False & Mask (Mask'First .. Mask'Last - 1);
         

         Result := Result or Mask;
      end loop;
      return Result;
   end Right_Adjacent;

   procedure Put (Collection : Bit_Collection) is
      use Ada.Text_IO;
   begin
      for Bit of Collection loop
         Put ((if Bit then '1' else '0'));
      end loop;
   end Put;

   function Value (Item : String) return Bit_Collection
   is
      Length : constant Bit_Number := Item'Length;
      Result : Bit_Collection (1 .. Length);
      Index  : Natural := Item'First;
   begin
      for R of Result loop
         R := (case Item (Index) is
                  when '0' | 'F' | 'f' => False,
                  when '1' | 'T' | 't' => True,
                  when others =>
                     raise Constraint_Error with "invalid input");
         Index := Index + 1;
      end loop;
      return Result;
   end Value;

   procedure Show (Bit_String : String; N : Natural)
   is
      B      : constant Bit_Collection := Value (Bit_String);
      R      : constant Bit_Collection := Right_Adjacent (B, N);
      Prefix : constant String         := "        ";
      use Ada.Text_IO;
   begin
      Put ("n =");          Put (N'Image);
      Put ("; Width e =");  Put (Bit_String'Length'Image);
      Put (":");            New_Line;
      Put (Prefix);  Put ("Input B: ");  Put (B);  New_Line;
      Put (Prefix);  Put ("Result : ");  Put (R);  New_Line;
      New_Line;
   end Show;

begin
   Show ("1000", 2);
   Show ("0100", 2);
   Show ("0010", 2);
   Show ("0000", 2);

   Show ("010000000000100000000010000000010000000100000010000010000100010010", 0);
   Show ("010000000000100000000010000000010000000100000010000010000100010010", 1);
   Show ("010000000000100000000010000000010000000100000010000010000100010010", 2);
   Show ("010000000000100000000010000000010000000100000010000010000100010010", 3);
end Set_Right_Bits;