shifting a 32bit in a array!!!!
 CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com

# Thread: shifting a 32bit in a array!!!!

1. Junior Member
Join Date
Feb 2012
Posts
18

## 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!!!!

2. Elite Member
Join Date
Oct 2006
Location
Sweden
Posts
3,649

## 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?

3. Junior Member
Join Date
Feb 2012
Posts
18

## Re: shifting a 32bit in a array!!!!

array contains binary number of length-32!!!! how to do shifting of those binary numbers!!!!

4. Junior Member
Join Date
Feb 2012
Posts
18

## 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 04:05 PM.

5. Elite Member Power Poster
Join Date
Oct 2007
Location
Seattle, WA
Posts
10,895

## 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.

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::endl;

// drive on
for (int idx = 0; idx < COUNTOF(p); idx++)
{
int begval = shift ? 0 : *(v.end() - 1);
ShiftVect(v, begval);  // 0              - shift
// *(v.end() - 1) - rotate
DumpVect(v);
}

return 0;
}```
Code:
```E:\Temp\643>643
*[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

*[32] 0,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
*[32] 0,0,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
*[32] 0,0,0,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
*[32] 0,0,0,0,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
*[32] 0,0,0,0,0,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
*[32] 0,0,0,0,0,0,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
*[32] 0,0,0,0,0,0,0,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
*[32] 0,0,0,0,0,0,0,0,1,0,1,1,0,1,0,1,0,0,1,0,1,0,0,1,1,0,1,0,0,1,0,1
*[32] 0,0,0,0,0,0,0,0,0,1,0,1,1,0,1,0,1,0,0,1,0,1,0,0,1,1,0,1,0,0,1,0
*[32] 0,0,0,0,0,0,0,0,0,0,1,0,1,1,0,1,0,1,0,0,1,0,1,0,0,1,1,0,1,0,0,1
*[32] 0,0,0,0,0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0,1,1,0,1,1,0,1,1,0,1,0,1,1,0,1,0,1,0,0
*[32] 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,1,0
*[32] 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,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```

7. Junior Member
Join Date
Feb 2012
Posts
18

## 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?

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 03:14 AM.

9. Junior Member
Join Date
Feb 2012
Posts
18

## 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!!!!!

10. Elite Member Power Poster
Join Date
Apr 1999
Posts
27,449

## 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

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.

12. Elite Member Power Poster
Join Date
Aug 2000
Location
New York, NY, USA
Posts
5,656

## 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?

13. Elite Member Power Poster
Join Date
Oct 2007
Location
Seattle, WA
Posts
10,895

## 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).

14. Elite Member Power Poster
Join Date
Nov 2003
Location
Florida
Posts
12,518

## 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.

15. Elite Member
Join Date
Oct 2006
Location
Sweden
Posts
3,649

## 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
•