Advertisement
Terrah

binary rotational shift example

Jun 27th, 2015
300
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.12 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <Windows.h>
  3.  
  4. typedef int bool;
  5. #define true 1
  6. #define false 0
  7.  
  8. bool GetBit(unsigned char data, int bit){
  9. return (data >> bit) & 1 == 1;
  10. }
  11.  
  12. //Binary right shift (rotate)
  13. char BRS(unsigned char data){
  14. return (data >> 1) | (data << (8 - 1));
  15. }
  16.  
  17. //Binary left shift (rotate)
  18. char BLS(unsigned char data){
  19. return (data << 1) | (data >> (8 - 1));
  20. }
  21.  
  22. void PrintByte(unsigned char data){
  23. int n;
  24. //left to right, least significant bit first, so this is reverse
  25. for (n = 7; n >= 0; n--){
  26. printf("%d", GetBit(data, n));
  27. }
  28. }
  29.  
  30. void main(){
  31.  
  32. int n;
  33. unsigned char test = 1;
  34.  
  35. puts("BLS:");
  36. PrintByte(test);
  37. puts("\n-");
  38. for (n = 0; n < 8; n++){
  39. test = BLS(test);
  40. PrintByte(test);
  41. puts(" ");
  42. }
  43.  
  44. puts("\nBRS:");
  45.  
  46. test = 1;
  47. PrintByte(test);
  48. puts("\n-");
  49. for (n = 0; n < 8; n++){
  50. test = BRS(test);
  51. PrintByte(test);
  52. puts(" ");
  53. }
  54.  
  55. unsigned long number = 0x5555555555555555;
  56.  
  57. unsigned char * raw = &number;
  58.  
  59. puts("\n-");
  60.  
  61. //Read the long byte for byte backwards
  62. for (n = sizeof(number)-1; n >=0; n--){
  63. PrintByte(raw[n]);
  64. }
  65.  
  66. _getch();
  67. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement