3. 定义3个16位无符号数,比如000CH,004EH,0FFFEH,用循环结构实现将每个数首尾反转后重新保存在原位置。如原数为1111000011001010,反转后为0101001100001111。
时间: 2024-02-19 19:59:08 浏览: 29
好的,您的问题是如何用循环结构实现将每个16位无符号数首尾反转后重新保存在原位置。我来给您解答。
首先,我们需要定义三个16位无符号数,如下:
```c++
unsigned short num1 = 0x000C;
unsigned short num2 = 0x004E;
unsigned short num3 = 0xFFFE;
```
接下来,我们可以定义一个函数来实现将一个16位无符号数的首尾反转,代码如下:
```c++
unsigned short reverse(unsigned short num)
{
unsigned short result = 0;
for(int i = 0; i < 16; i++)
{
result <<= 1;
result |= num & 1;
num >>= 1;
}
return result;
}
```
该函数使用了一个循环来遍历原数的每一位,将其逆序存储到一个新的变量中,并返回该变量。接下来我们可以用该函数对三个数进行反转,代码如下:
```c++
num1 = reverse(num1);
num2 = reverse(num2);
num3 = reverse(num3);
```
最后,我们将三个数重新保存在原位置,代码如下:
```c++
unsigned short temp;
temp = num1;
num1 = num1;
num1 = temp;
temp = num2;
num2 = num3;
num3 = temp;
```
这样,就完成了将三个16位无符号数首尾反转后重新保存在原位置的操作。
相关推荐
![rar](https://img-home.csdnimg.cn/images/20210720083606.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![pptx](https://img-home.csdnimg.cn/images/20210720083543.png)
![exe](https://img-home.csdnimg.cn/images/20210720083343.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![doc](https://img-home.csdnimg.cn/images/20210720083327.png)