C++ Tutorial/Data Types/shift

Материал из C\C++ эксперт
Перейти к: навигация, поиск

Demonstrate bitwise left shift

#include <iostream>
using std::cout;
using std::cin;
using std::endl;
#include <iomanip>
using std::setw;
void displayBits( unsigned ); // prototype
int main()
{
   unsigned number1 = 960;
   cout << "The result of left shifting\n";
   displayBits( number1 );
   cout << "8 bit positions using the left-shift operator is\n";
   displayBits( number1 << 8 );
   return 0;
}
//Quote from
//C++ How to Program (5th Edition) (How to Program) (Paperback)
//by Harvey & Paul) Deitel & Associates
//Publisher: Prentice Hall; 5 edition (January 5, 2005)
//Language: English
//ISBN-10: 0131857576
//ISBN-13: 978-0131857575
void displayBits( unsigned value ) {
   const int SHIFT = 8 * sizeof( unsigned ) - 1;
   const unsigned MASK = 1 << SHIFT;
   cout << setw( 10 ) << value << " = ";
   for ( unsigned i = 1; i <= SHIFT + 1; i++ ) 
   {
      cout << ( value & MASK ? "1" : "0" );
      value <<= 1;
      if ( i  8 == 0 )
         cout << " ";
   }
   cout << endl;
}
The result of left shifting
       960 = 00000000 00000000 00000011 11000000
8 bit positions using the left-shift operator is
    245760 = 00000000 00000011 11000000 00000000

Demonstrate bitwise right shift

#include <iostream>
using std::cout;
using std::cin;
using std::endl;
#include <iomanip>
using std::setw;
void displayBits( unsigned ); // prototype
int main()
{
   unsigned number1 = 960;
   cout << "\nThe result of right shifting\n";
   displayBits( number1 );
   cout << "8 bit positions using the right-shift operator is\n";
   displayBits( number1 >> 8 );

   return 0;
}
//Quote from
//C++ How to Program (5th Edition) (How to Program) (Paperback)
//by Harvey & Paul) Deitel & Associates
//Publisher: Prentice Hall; 5 edition (January 5, 2005)
//Language: English
//ISBN-10: 0131857576
//ISBN-13: 978-0131857575
void displayBits( unsigned value ) {
   const int SHIFT = 8 * sizeof( unsigned ) - 1;
   const unsigned MASK = 1 << SHIFT;
   cout << setw( 10 ) << value << " = ";
   for ( unsigned i = 1; i <= SHIFT + 1; i++ ) 
   {
      cout << ( value & MASK ? "1" : "0" );
      value <<= 1;
      if ( i  8 == 0 )
         cout << " ";
   }
   cout << endl;
}
The result of right shifting
       960 = 00000000 00000000 00000011 11000000
8 bit positions using the right-shift operator is
         3 = 00000000 00000000 00000000 00000011

Left rotate functions for byte values

/*
Quote from: C++: A Beginner"s Guide, Second Edition 
# Publisher: McGraw-Hill Osborne Media; 2 edition (December 3, 2003)
# Language: English
# ISBN-10: 0072232153
# ISBN-13: 978-0072232158
*/

#include <iostream> 
using namespace std; 
 
unsigned char lrotate(unsigned char val, int n); 
void show_binary(unsigned int u); 
 
int main() 
{ 
  char ch = "A"; 
 
  cout << "Original value in binary:\n"; 
  show_binary(ch); 
 
  cout << "Rotating left 8 times:\n"; 
  for(int i=0; i < 8; i++) { 
    ch = lrotate(ch, 1); 
    show_binary(ch); 
  } 
  return 0; 
} 
 
unsigned char lrotate(unsigned char val, int n) 
{ 
  unsigned int t; 
 
  t = val; 
 
  for(int i=0; i < n; i++) { 
    t = t << 1; 
 
    /* If a bit shifts out, it will be in bit 8 
       of the integer t. If this is the case, 
       put that bit on the right side. */ 
    if(t & 256) 
      t = t | 1; // put a 1 on the right end 
  } 
 
  return t; // return the lower 8 bits. 
} 
 
// Display the bits within a byte. 
void show_binary(unsigned int u) 
{ 
  int t; 
 
  for(t=128; t>0; t = t/2) 
    if(u & t) cout << "1 "; 
    else cout << "0 "; 
 
  cout << "\n"; 
}
Original value in binary:
0 1 0 0 0 0 0 1
Rotating left 8 times:
1 0 0 0 0 0 1 0
0 0 0 0 0 1 0 1
0 0 0 0 1 0 1 0
0 0 0 1 0 1 0 0
0 0 1 0 1 0 0 0
0 1 0 1 0 0 0 0
1 0 1 0 0 0 0 0
0 1 0 0 0 0 0 1

Right rotate functions for byte values

/*
Quote from: C++: A Beginner"s Guide, Second Edition 
# Publisher: McGraw-Hill Osborne Media; 2 edition (December 3, 2003)
# Language: English
# ISBN-10: 0072232153
# ISBN-13: 978-0072232158
*/
 
#include <iostream> 
using namespace std; 
 
unsigned char rrotate(unsigned char val, int n); 
void show_binary(unsigned int u); 
 
int main() 
{ 
  char ch = "T"; 
 
  cout << "Original value in binary:\n"; 
  show_binary(ch); 
 
  cout << "Rotating right 8 times:\n"; 
  for(int i=0; i < 8; i++) { 
    ch = rrotate(ch, 1); 
    show_binary(ch); 
  } 
 
  return 0; 
} 
 
// Right-rotate a byte n places. 
unsigned char rrotate(unsigned char val, int n) 
{ 
  unsigned int t; 
 
  t = val; 
 
  // First, move the value 8 bits higher. 
  t = t << 8; 
 
  for(int i=0; i < n; i++) { 
    t = t >> 1; 
 
    /* If a bit shifts out, it will be in bit 7 
       of the integer t. If this is the case, 
       put that bit on the left side. */ 
    if(t & 128)  
      t = t | 32768; // put a 1 on left end 
  } 
 
  /* Finally, move the result back to the 
     lower 8 bits of t. */ 
  t = t >> 8; 
 
  return t; 
} 
 
// Display the bits within a byte. 
void show_binary(unsigned int u) 
{ 
  int t; 
 
  for(t=128; t>0; t = t/2) 
    if(u & t) cout << "1 "; 
    else cout << "0 "; 
 
  cout << "\n"; 
}
Original value in binary:
0 1 0 1 0 1 0 0
Rotating right 8 times:
0 0 1 0 1 0 1 0
0 0 0 1 0 1 0 1
1 0 0 0 1 0 1 0
0 1 0 0 0 1 0 1
1 0 1 0 0 0 1 0
0 1 0 1 0 0 0 1
1 0 1 0 1 0 0 0
0 1 0 1 0 1 0 0

Shift left

#include <iostream>
using namespace std;
void show_binary(unsigned int u);
int main()
{
  int i=1, t;

  for(t=0; t < 8; t++) {
    show_binary(i);
    i = i << 1 ;
  }
  return 0;
}
// Display the bits within a byte.
void show_binary(unsigned int u)
{
  int t;
  for(t=128; t>0; t=t/2)
    if(u & t) cout << "1 ";
    else cout << "0 ";
  cout << "\n";
}
0 0 0 0 0 0 0 1
0 0 0 0 0 0 1 0
0 0 0 0 0 1 0 0
0 0 0 0 1 0 0 0
0 0 0 1 0 0 0 0
0 0 1 0 0 0 0 0
0 1 0 0 0 0 0 0
1 0 0 0 0 0 0 0

Shift right

#include <iostream> 
using namespace std; 
 
void show_binary(unsigned int u); 
 
int main() 
{ 
  int i=1, t; 
 
  
  for(t=0; t < 8; t++) { 
    i = i >> 1; 
    show_binary(i); 
  } 
 
  return 0; 
} 
// Display the bits within a byte. 
void show_binary(unsigned int u) 
{ 
  int t; 
 
  for(t=128; t>0; t=t/2) 
    if(u & t) cout << "1 "; 
    else cout << "0 "; 
 
  cout << "\n"; 
}
0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0