CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 15 of 24

Hybrid View

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

  9. #9
    Join Date
    Jun 2010
    Location
    Germany
    Posts
    2,675

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

    Quote Originally Posted by S_M_A View Post
    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.

  10. #10
    Join Date
    Oct 2006
    Location
    Sweden
    Posts
    3,654

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

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