无需操作员即可旋转左右程序? (<<>>) [英] Rotate right and left program without operators? (<<, >>)

查看:79
本文介绍了无需操作员即可旋转左右程序? (<<>>)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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.







(In C programming.)
Request





我尝试了什么:



我只是从用户那里拿二进制代码但是无法计算轮换。



What I have tried:

I just take binary codes from user but cant figure the rotation.

推荐答案

没有旋转C / C ++中的(循环移位)操作。



您可以使用自己喜欢的搜索引擎来查找实施。一个很好的链接是 C / C ++中的安全,高效和便携式旋转 - 嵌入学术界 [ ^ ]。



要旋转存储在数组中的值,只需迭代元素并将旋转应用于每个元素。
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.


如果你的意思是你正在直接从用户那里读取字符,所以你正在处理一个字符数组,然后不,你不能使用移位操作符来旋转任何东西。

你有选择:

1)将用户输入转换为数字,并使用移位运算符旋转它。

2)手动旋转字节。



第一个是微不足道的,你应该能够轻松应对这个问题。

当你想到它时,第二个也很简单。但是......这听起来像家庭作业,所以没有代码!

要正确旋转一组字符,请从右端开始。

  • 删除最后一个数字并将其保存在一个临时寄存器中。
  • 将剩余的字节复制到一个更大的数组索引中。
  • 当你将它们全部移动时,将你保存的字符放入第一个位置。
  • 向左旋转的操作大致相同,但方向相反。
    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.


    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;;
    	}


    这篇关于无需操作员即可旋转左右程序? (&lt;&lt;&gt;&gt;)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

    查看全文
    登录 关闭
    扫码关注1秒登录
    发送“验证码”获取 | 15天全站免登陆