Click here to Skip to main content
15,880,796 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
C#
For rotate binary numbers like 10101011, ı want to rotate the binary code n times(I am taking this from user). But ı dont know how can take array’s from user by 2d array or just normal array? And how can ı rotate witohut <<,>> ı am kind of a stuck in that.


C#
(In C programming.)
Request


What I have tried:

I just take binary codes from user but cant figure the rotation.
Posted
Updated 26-Oct-16 4:14am

There is no rotate (circular shift) operation in C/C++.

You may use your favorite search engine to find implementations. A good link is Safe, Efficient, and Portable Rotate in C/C++ – Embedded in Academia[^].

To rotate values stored in an array just iterate over the elements and apply the rotation to each element.
 
Share this answer
 
If you mean you are reading the characters direct from the user, so you are dealing with a char array, then no, you can't use the shift operators to rotate anything.
You have options:
1) Convert the user input to a number, and rotate that using the shift operators.
2) Manually rotate the bytes.

The first is trivial and you should be able to cope with that pretty easily.
The second is also simple, when you think about it. But ... this sounds like homework, so no code!
To rotate an array of characters right, start at the right hand end.
  • Remove the last digit and save it in a temporary register.
  • Loop through the remaining bytes copying each into the array index one greater.
  • When you have moved them all, put the character you saved in the first position.
  • Rotating left is much the same operation, but with the directions reversed.
     
    Share this answer
     
    Comments
    JaxTeller61 26-Oct-16 10:20am    
    Okay first of all thanks for helping. I have one tiny question. When ı take the reading characters direct from user. User must be enter like this 1 0 1 0 0 1 but Teacher cant accept like this ı have trouble with ho can ı get like 101001 into char array.
    «_Superman_» 27-Oct-16 2:59am    
    Read it as a string (character array) instead of character by character - scanf("%s", arr);
    JaxTeller61 27-Oct-16 18:02pm    
    I am just trying to asking //read binary number to an array but cant define my problem but ı can figure it np.
    C#
    printf("\nEnter 8 bit binary number to perform shift and rotate operations:  ");
    	scanf_s("%d",&bin3);
    	b=bin3;
    	//read binary number to an array
    	//from lsb to msb
    	while(m<8)
    	{
    		shift_array[m]=bin3%10;
    		bin3=bin3/10;
    		m=m+1;
    	}
    	printf("\nHow many times you want to shift/rotate ");
    	scanf_s("%d",&j);
    	n=j;
    
    	//circular shift right
    
    	printf("\n\nCircular Shift/rotation (Right)\n");
    
    	while(j>0)
    	{
    		printf("%d",shift_array[j-1]);
    
    		j=j-1;;
    	}
    	bin3=b/pow(10.0,n);
    	if(bin3!=0){
    		printf("%d ",bin3);
    	}
    
    	//circular shift left
    	j=n;
    	printf("\n\nCircular Shift/rotation (	Left)\n");
    	while(j<8)
    	{
    		printf("%d",shift_array[7-j]);
    
    		j=j+1;
    	}
    	j=n;
    	while(j>0)
    	{
    		printf("%d",shift_array[7-k]);
    		k=k+1;
    		j=j-1;;
    	}
     
    Share this answer
     
    Comments
    Patrice T 27-Oct-16 23:35pm    
    Use Improve question to update your question.
    So that everyone can pay attention to this information.

    This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



    CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900