lundi 20 avril 2020

I am unable to understand the output to my c++ code

I was doing a problem the part of the problem is to shift the a given string of 0 and 1 of a given number n to a given amount (here sft variable taken). T queries. I was getting error in right shift while left shift had no problem. The whole code is below -

#include<iostream>
#include<bitset>
using namespace std;
int main()
{
    const int m=16;
    int n,t;
    cin>>t;
    int sft;
    char ch;
    int arr[m];
    while(t--)
    {
        cin>>n;
        cin>>sft;
        cin>>ch;
        bitset<m>bt(n);
        cout<<bt<<endl;
        if(ch=='R')
        {
            for(int i=0;i<m;i++)
            {
                arr[i]=bt[((i+sft)%m)];                               // problem is here
                // cout<<((i+sft)%m)<<"-"<<bt[((i+sft)%m)]<<" ";      // to check what is happening
            }

        }}}

PROBLEM - The problem is that for a given position in bt string , I am not getting what I am supposed to get it is giving wrong bit I do not know why?

input :
1(queries)
16(number) 3 (sft) R(right)

Output
bt string = 0000000000010000
Position-Bit in bt = 3-0 4-1 5-0 6-0 7-0 8-0 9-0 10-0 11-0 12-0 13-0 14-0 15-0 0-0 1-0 2-0

Aucun commentaire:

Enregistrer un commentaire