-
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, 03:05 PM
#8
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;
-
February 17th, 2012, 03:37 PM
#9
Re: shifting a 32bit in a array!!!!
Originally Posted by S_M_A
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;
Not perhaps rather something like:
Code:
for(int i = 31; i > 0; i--)
{
p[i]=p[i-1];
}
p[0] = 0;
?
Based on the assumption, of course, that "left" means the end with the lower index, i.e. 0, just like it's written in the initializer.
Now I just hope you didn't just want to give the OP something to debug and now I've spoiled it...
I was thrown out of college for cheating on the metaphysics exam; I looked into the soul of the boy sitting next to me.
This is a snakeskin jacket! And for me it's a symbol of my individuality, and my belief... in personal freedom.
-
February 18th, 2012, 05:43 AM
#10
Re: shifting a 32bit in a array!!!!
No you didn't spoil anything Eri523. I was just to tired. Posting on a friday evening after a dinner with wine isn't to be recommended either...
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
|