-
February 16th, 2012, 08:48 AM
#1
shifting a 32bit in a array!!!!
how to shift a 32bit array in C++? for this i need a c++ programming!!!
pl help me in this!!!!
-
February 16th, 2012, 08:57 AM
#2
Re: shifting a 32bit in a array!!!!
Shift what and how? Do you think of the array as an integer > 32 bits that should be shifted?
-
February 16th, 2012, 04:09 PM
#3
Re: shifting a 32bit in a array!!!!
array contains binary number of length-32!!!! how to do shifting of those binary numbers!!!!
-
February 16th, 2012, 04:13 PM
#4
shifting a 32bit in a array!!!!
array contains binary number of length-32!!!! how to do shifting of those binary numbers!!!![it has to shift from left to right].
int p[32]={1,0,1,1,0,1,0,1,0,0,1,0,1,0,0,1,1,0,1,0,0,1,0,1,1,0,1,1,0,1,1,0}
how to do shifting of this array?
Last edited by hemanth144; February 16th, 2012 at 05:05 PM.
-
February 16th, 2012, 04:14 PM
#5
Re: shifting a 32bit in a array!!!!
Show us some code. It still isn't clear exactly what the type or size of this array is.
-
February 17th, 2012, 02:29 AM
#6
Re: shifting a 32bit in a array!!!!
Not sure if vector is an appropriate replacement in your case...
Code:
#include <vector>
#include <string>
#include <iostream>
#define COUNTOF(x) (sizeof(x)/sizeof(x[0]))
template <typename T>
void DumpVect(std::vector<T>& v)
{
// dump size
std::cout << "*[" << v.size() << "] ";
// dump elements
for (int idx = 0; idx < v.size(); idx++)
{
if (idx)
std::cout << ",";
std::cout << v.at(idx);
}
std::cout << std::endl;
}
template <typename T>
void ShiftVect(std::vector<T>& v, T begval)
{
// shift left to right
v.erase(v.end() - 1); // erase rightmost element
v.insert(v.begin(), begval); // insert begval as the first element
}
int main(int argc, char** argv)
{
bool shift = true;
if (argc > 1)
{
std::string action = argv[1];
if (action == "rotate")
shift = false;
}
// source array
int p[32]={1,0,1,1,0,1,0,1,0,0,1,0,1,0,0,1,1,0,1,0,0,1,0,1,1,0,1,1,0,1,1,0};
std::vector<int> v;
// fill the vector
for (int idx = 0; idx < COUNTOF(p); idx++)
v.push_back(p[idx]);
DumpVect(v);
std::cout << std::end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,0,1,1,0,1,0,0,1,0,1,1,0,1,1,0,1,1,0,1,0,1,1,0,1,0,1
*[32] 1,0,0,1,0,1,0,0,1,1,0,1,0,0,1,0,1,1,0,1,1,0,1,1,0,1,0,1,1,0,1,0
*[32] 0,1,0,0,1,0,1,0,0,1,1,0,1,0,0,1,0,1,1,0,1,1,0,1,1,0,1,0,1,1,0,1
*[32] 1,0,1,0,0,1,0,1,0,0,1,1,0,1,0,0,1,0,1,1,0,1,1,0,1,1,0,1,0,1,1,0
*[32] 0,1,0,1,0,0,1,0,1,0,0,1,1,0,1,0,0,1,0,1,1,0,1,1,0,1,1,0,1,0,1,1
*[32] 1,0,1,0,1,0,0,1,0,1,0,0,1,1,0,1,0,0,1,0,1,1,0,1,1,0,1,1,0,1,0,1
*[32] 1,1,0,1,0,1,0,0,1,0,1,0,0,1,1,0,1,0,0,1,0,1,1,0,1,1,0,1,1,0,1,0
*[32] 0,1,1,0,1,0,1,0,0,1,0,1,0,0,1,1,0,1,0,0,1,0,1,1,0,1,1,0,1,1,0,1
*[32] 1,0,1,1,0,1,0,1,0,0,1,0,1,0,0,1,1,0,1,0,0,1,0,1,1,0,1,1,0,1,1,0
Best regards,
Igor
-
February 17th, 2012, 03:27 AM
#7
Re: shifting a 32bit in a array!!!!
array contains binary number of length-32!!!! how to do shifting of those binary numbers!!!![it has to shift from left to right].
int p[32]={1,0,1,1,0,1,0,1,0,0,1,0,1,0,0,1,1,0,1,0,0,1,0,1,1,0,1,1,0,1,1,0}
how to do shifting of this array?
-
February 17th, 2012, 04:09 AM
#8
Re: shifting a 32bit in a array!!!!
You really think that repeating your question without any additional explanation can help?
You go in loop from end to beginning setting p[i] = p[i-1]; finally p[0] = 0; (or any other required)
Last edited by Igor Vartanov; February 17th, 2012 at 04:14 AM.
Best regards,
Igor
-
February 17th, 2012, 04:52 AM
#9
Re: shifting a 32bit in a array!!!!
Originally Posted by Igor Vartanov
You really think that repeating your question without any additional explanation can help?
You go in loop from end to beginning setting p[i] = p[i-1]; finally p[0] = 0; (or any other required)
Sorry for repeating a question!!!!
for(i=0;i<31;i++)
{
p[i]=p[i-1];
cout<<p[i];
} // if i use this code the bits be shifted from left to right?
pl help me in this!!!!!
-
February 17th, 2012, 05:39 AM
#10
Re: shifting a 32bit in a array!!!!
Originally Posted by hemanth144
Sorry for repeating a question!!!!
for(i=0;i<31;i++)
{
p[i]=p[i-1];
cout<<p[i];
} // if i use this code the bits be shifted from left to right?
Why not write the program and see if it works?
Seriously, I can't believe the number of new posters who either refuse to debug their code and want one of us to do that job for them, or they can't just write their program and then see if it works or not.
Regards,
Paul McKenzie
-
February 17th, 2012, 06:15 AM
#11
Re: shifting a 32bit in a array!!!!
for(i=0;i<31;i++)
{
p[i]=p[ i-1];
cout<<p[i];
} // if i use this code the bits be shifted from left to right?
As far as I can tell, the code is going to crash immediately on the first iteration because of index being out of bounds. And even started from 1, the code just replicates the first array element (not bits!) through the rest of the array. So the answer is: No they won't.
Best regards,
Igor
-
February 17th, 2012, 09:26 AM
#12
Re: shifting a 32bit in a array!!!!
Originally Posted by hemanth144
how to do shifting of this array?
Originally Posted by hemanth144
how to do shifting of those binary numbers!!!!
So what are you shifting? Elements in array or their content?
Vlad - MS MVP [2007 - 2012] - www.FeinSoftware.com
Convenience and productivity tools for Microsoft Visual Studio:
FeinWindows - replacement windows manager for Visual Studio, and more...
-
February 17th, 2012, 10:35 AM
#13
Re: shifting a 32bit in a array!!!!
You need to elaborate on what you expect to happen at the boundary conditions (first and last element of the array).
-
February 17th, 2012, 10:44 AM
#14
Re: shifting a 32bit in a array!!!!
Originally Posted by hemanth144
array contains binary number of length-32!!!! how to do shifting of those binary numbers!!!![it has to shift from left to right].
int p[32]={1,0,1,1,0,1,0,1,0,0,1,0,1,0,0,1,1,0,1,0,0,1,0,1,1,0,1,1,0,1,1,0}
how to do shifting of this array?
Exclamation points don't add clarity to your post, no matter how many you use. Since nobody understood your question the first time, try rewording it.
-
February 17th, 2012, 03:05 PM
#15
Re: shifting a 32bit in a array!!!!
Frankly, why is it so impossible to spend a few minutes to write a question that clearly states the issue?
Assuming that you want to shift
int p[32]={1,0,1,1,0,1,0,1,0,0,1,0,1,0,0,1,1,0,1,0,0,1,0,1,1,0,1,1,0,1,1,0} from left to right this should do it
Code:
for(int i=1;i<31;i++)
{
p[i]=p[i-1];
}
p[31] = 0;
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|