fork download
  1. #include <cstdio>
  2.  
  3. using namespace std;
  4.  
  5. inline void printBits(unsigned a){
  6. for(int i = 31; i >= 0; --i)
  7. printf(((a >> i) & 1) ? "1" : "0");
  8. puts("");
  9. }
  10.  
  11. template<class X> void testShift(X a){
  12. printf("Before:\n");
  13. printBits(a);
  14. a = (a >> 1);
  15. printf("After:\n");
  16. printBits(a);
  17. puts("");
  18. }
  19.  
  20. int main(){
  21.  
  22. printf("Signed 15\n");
  23. testShift(15);
  24. printf("Signed -15\n");
  25. testShift(-15);
  26.  
  27. printf("Unsigned 15\n");
  28. testShift(unsigned(15));
  29. printf("Unsigned -15\n");
  30. testShift(unsigned(-15));
  31.  
  32. return 0;
  33. }
  34.  
Success #stdin #stdout 0.02s 2724KB
stdin
Standard input is empty
stdout
Signed 15
Before:
00000000000000000000000000001111
After:
00000000000000000000000000000111

Signed -15
Before:
11111111111111111111111111110001
After:
11111111111111111111111111111000

Unsigned 15
Before:
00000000000000000000000000001111
After:
00000000000000000000000000000111

Unsigned -15
Before:
11111111111111111111111111110001
After:
01111111111111111111111111111000