Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 2

SANJIB BISWAL

REGD – 2341002219
SEC-23412K1

Problem1_RoatateRight=
HERE WE HAVE CREATED A FUNCTION NAMED roateRightBy2Bits and here
are the steps below how it is working:-

1)HERE WE HAVE BASICALLY USED AN ARRAY NAMED ROTATED IN THE FIRST JUST TO ASSIGN OR YOU
CAN SAY TO STORE OUR ROTATED DATA THAT WE HAVE OBTAINED AFTER USING LOOP

2) NEXT COMES THE LOOP THAT IS STARTING FROM 0 INDEX AND ENDING IN THE LAST INDEX AND
INCREMENTING BY 1

(INSIDE THE LOOP)

 SO AT FIRST WE ARE USING OUR LOOP INDEX DATA AND DOING RIGHT SHIFT BY 2 AND THEN
AGAIN DOING LEFT SHIFT BY USING (INTEGER.SIZE-2) USING 2 AS IN QUESTION IT HAS BEEN
MENTIONED TO USE 2…AND THEN FINALLY WE ARE ADDING IT OR YOU CAN SAY JOINING IT
USING BITWISE OR…
 SO WHAT EXACTLY IS HAPPENING INSIDE The LOOP?
(WE HAVE SEEN ROTATION OF ARRAY THERE WE WERE BREAKING THE ARRAY IN 2 PARTS , 1 ST
WAS 0 TO D INDEX TILL WE HAVE TO ROTATE AND THEN D TO N INDEX AND WHAT WE WERE
DOING IS REVERSING BOTH ARRAY AND THEN FINALLY REVERSING THE FULL ARRAY BY
COMBINING IT…..)-ANALOGY
SIMILARLY HERE WE ARE FIRST MAKING BITWISE RIGHT SHIFT BY 2 AND WHAT IS HAPPENING
IS THE FIRST 2 ARE COMING AT LAST AND FINALLY TO BRING THE LAST 2 IN FIRST WE USED
INTEGER.SIZE-2 LEFT SHIFT TO BRING IT TO THE FIRST AND THEN WE COMBINE IT USING
BITWISE OR(WHAT DIFFERENCE HERE IS WE ARE DOING ROTATION INSIDE EACH INTEGER IN
BINARY FORMAT AND THE BITWISE OPERATOR IS GIVING IT BACK IN INTEGER FORMAT AFTER
DOING ROTATION)
 NEXT INSIDE THE LOOP ONLY WE HAVE USED A INBUILT FUNCTION
INTEGER.BINARYTOSTRING().
 USING THIS METHOD WE HAVE FIRST CONVERTED THE ORIGINAL ARRAY TO BINARY AND
PRINTED AS MENTIONED IN QUESTION.
 NEXT AGAIN BY USING THIS METHOD WE HAVE CONVERTED OUR ROTATED ARRAY TO BINARY
FORM AND PRINTED IT.

3) FINALLY WE CALL THE FUNCTION JINSIDE THE MAIN FUNCTION AND THE CODE IS DONE….

(NOTE- WE CAN ALSO MAKE ON MORE FUNCTION WHERE WE CAN MAKE OUR OWN CODE TO
CONVERT DECIMAL TO BINARY BUT HERE AS ALREADY MENTIONED SO NO PROBLEM)

_____________________________☓ ___________________________________________________

Problem2_ConvertToAnyBase
HERE WE HAVE CREATED A FUNCTION NAMED ConvertTOAnyBase and here
are the steps below how it is working:-

PROJECT SEMESTER-1
SANJIB BISWAL
REGD – 2341002219
SEC-23412K1

 So inside this function we will create a string as there might be a hexadecimal which might
have A-F from 10 to 14 so we have taken a string and initialized it as empty.
 Next we have created a char array and inside it we have stored 0 to F and after that
used .toCharArray() to make the string an array.
 Next we have created a loop and condition is n>0 meaning is the loop will keep on running
until the no becomes zero
 Next inside the loop we have used last digit where we have used n%b (as we use mostly n
%10 to get last digit or n%2 to get remainder as we were doing in decimal to binary)
 Next we have used base=

Base = ch[lastdigit] + base;


which states that when the lastdigit will come anything it will come inside the ch array and
the array will use that value and concatenate it with base which is initialized it as empty here……
AND FINALLY WE WAVE DONE n/=b; to remove the number used ….

 And finally return the base…


 AND FINALLY WE WILL CALL THE FUNCTION IN THE MAIN FUNCTION AND WE
WILL GET THE DESIRED VALUE…..

PROJECT SEMESTER-1

You might also like