Click here to Skip to main content
15,892,537 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
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);

}
Posted
Updated 14-Jul-17 10:57am
Comments
CPallini 14-Jul-17 8:54am    
What is exactly your requirement? Please provide an example of input and expected output.
Dave Kreskowiak 14-Jul-17 17:23pm    
"Not working as desired" is not a proper problem description. What is the input, what is the expected output, and what did you get?

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.
 
Share this answer
 
Quote:
The shifting operation is not working as desired.

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.
 
Share this answer
 
v2

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