左移字符串的字符乘以用户指定的x位 [英] Left shifting the characters of a string by x bits specified by the user

查看:67
本文介绍了左移字符串的字符乘以用户指定的x位的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图将字符串的移位字符保留为用户指定的x位。换档操作无法正常工作。



我尝试过:



I am trying to left shift characters of a string by x bits specified by the user. The shifting operation is not working as desired.

What I have tried:

#include <iostream>
#include <stdio.h>
using namespace std;

void display(char *name, int l)
{
	int i;
	for(i=0;i<=l;i++)
		cout<<name[i];
}
void shiftop(char *name, int shiftbit, int l) //l=stringlength
{
 int sb,i=1;
 char temp;
 
 for(sb=shiftbit+1;sb<=l;sb++)
 {
 	temp=name[i];
 	name[i]=name[sb];
 	name[sb]=temp;
 	i++;

 }
 display(name,l);
}
int main(){

int l,s;
char name[30];
cout<<"Enter the string";
gets(name);
cout<<"shift bit ";
cin>>s;
l=sizeof(name);
shiftop(name,s,l);

}

推荐答案

那是因为你的代码与位没有任何关系:它交换字符, 是全部。位移是在一个字节内移动数据的过程,也可能是在字节之间移动。

因此,如果从值42开始 - 十六进制2A,二进制00101010 - 并将其移位2位,则得到二进制10101000 - 十六进制A8,十进制168 - 当你移位时,在前一个字节的底部插入两个零位。



这个代码的作用就是交换整个字符大概就是你需要的东西。

看看<< & 二元运算符,但你需要使用无符号字符来正确执行此操作。
That's because your code has nothing at all to do with bits: it swaps characters about, is all. Bit shifting is the process of moving data within a byte, and possibly between bytes.
So if you start with the value 42 - Hex 2A, Binary 00101010 - and left shift it 2 bits, you get binary 10101000 - hex A8, decimal 168 - and two zero bits to insert at the bottom of the previous byte when you shift that.

What that code does is just swap whole characters about and is nothing like what you need.
Look at the << and the & binary operators, but you will need unsigned chars to do this properly.


引用:

移位操作没有按预期工作。

The shifting operation is not working as desired.



这不是提供信息!我们不知道什么是期望的。您显示的代码与位移无关。

只有您知道需要什么,使用调试器查看您的代码正在做什么,它可以帮助您查看错误。



有一个工具可以让你看到代码在做什么,它的名字是调试器。它也是一个很好的学习工具,因为它向你展示了现实,你可以看到哪种期望与现实相符。

当你不明白你的代码在做什么或为什么它做它做的时候,答案就是答案是调试器

使用调试器查看代码正在执行的操作。只需设置断点并查看代码执行情况,调试器允许您逐行执行第1行并在执行时检查变量。



调试器 - 维基百科,免费的百科全书 [ ^ ]



掌握Visual Studio 2010中的调试 - 初学者指南 [ ^ ]

使用Visual Studio 2010进行基本调试 - YouTube [ ^ ]

调试器在这里向您展示您的代码正在做什么,您的任务是与什么进行比较应该这样做。

调试器中没有魔法,它没有找到错误,它只是帮助你。当代码没有达到预期的效果时,你就会接近一个错误。


This is not informative ! We have no idea about what is desired. The code you show is not related to bit shifting.
Only you Know what is desired, use the debugger to see what your code is doing, it may help you to see what is wrong.

There is a tool that allow you to see what your code is doing, its name is debugger. It is also a great learning tool because it show you reality and you can see which expectation match reality.
When you don't understand what your code is doing or why it does what it does, the answer is debugger.
Use the debugger to see what your code is doing. Just set a breakpoint and see your code performing, the debugger allow you to execute lines 1 by 1 and to inspect variables as it execute.

Debugger - Wikipedia, the free encyclopedia[^]

Mastering Debugging in Visual Studio 2010 - A Beginner's Guide[^]
Basic Debugging with Visual Studio 2010 - YouTube[^]
The debugger is here to show you what your code is doing and your task is to compare with what it should do.
There is no magic in the debugger, it don't find bugs, it just help you to. When the code don't do what is expected, you are close to a bug.


这篇关于左移字符串的字符乘以用户指定的x位的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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