CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Page 1 of 2 12 LastLast
Results 1 to 15 of 24
  1. #1
    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. #2
    Join Date
    Oct 2006
    Location
    Sweden
    Posts
    3,654

    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?
    Debugging is twice as hard as writing the code in the first place.
    Therefore, if you write the code as cleverly as possible, you are, by
    definition, not smart enough to debug it.
    - Brian W. Kernighan

    To enhance your chance's of getting an answer be sure to read
    http://www.codeguru.com/forum/announ...nouncementid=6
    and http://www.codeguru.com/forum/showthread.php?t=366302 before posting

    Refresh your memory on formatting tags here
    http://www.codeguru.com/forum/misc.php?do=bbcode

    Get your free MS compiler here
    https://visualstudio.microsoft.com/vs

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

  5. #5
    Lindley is offline 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. #6
    Join Date
    Nov 2000
    Location
    Voronezh, Russia
    Posts
    6,620

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

  7. #7
    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. #8
    Join Date
    Nov 2000
    Location
    Voronezh, Russia
    Posts
    6,620

    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

  9. #9
    Join Date
    Feb 2012
    Posts
    18

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

    Quote Originally Posted by Igor Vartanov View Post
    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. #10
    Join Date
    Apr 1999
    Posts
    27,449

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

    Quote Originally Posted by hemanth144 View Post
    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. #11
    Join Date
    Nov 2000
    Location
    Voronezh, Russia
    Posts
    6,620

    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

  12. #12
    Join Date
    Aug 2000
    Location
    New York, NY, USA
    Posts
    5,656

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

    Quote Originally Posted by hemanth144 View Post
    how to do shifting of this array?
    Quote Originally Posted by hemanth144 View Post
    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...

  13. #13
    Lindley is offline 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. #14
    GCDEF is offline Elite Member Power Poster
    Join Date
    Nov 2003
    Location
    Florida
    Posts
    12,635

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

    Quote Originally Posted by hemanth144 View Post
    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. #15
    Join Date
    Oct 2006
    Location
    Sweden
    Posts
    3,654

    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;
    Debugging is twice as hard as writing the code in the first place.
    Therefore, if you write the code as cleverly as possible, you are, by
    definition, not smart enough to debug it.
    - Brian W. Kernighan

    To enhance your chance's of getting an answer be sure to read
    http://www.codeguru.com/forum/announ...nouncementid=6
    and http://www.codeguru.com/forum/showthread.php?t=366302 before posting

    Refresh your memory on formatting tags here
    http://www.codeguru.com/forum/misc.php?do=bbcode

    Get your free MS compiler here
    https://visualstudio.microsoft.com/vs

Page 1 of 2 12 LastLast

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured