Advertisement
gbulmer

stackoverflow-9767686-C

Mar 20th, 2012
185
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 1.47 KB | None | 0 0
  1. /***
  2.  *  Compares two methods of testing bit 5
  3.  *  Clarify compiler behaviour for stackoverflow.com topic 9767686
  4.  *
  5.  *  main.c
  6.  *
  7.  *  Created by G Bulmer on 19/03/2012.
  8.  */
  9.  
  10. #include "dummy_printf.h"   // get rid of stdio.h
  11. #include "original_bit5.h"
  12. #include "my_bit5.h"
  13.  
  14. int main (int argc, const char * argv[]) {
  15.     int number = 16;
  16.    
  17.     original_bit5(number);
  18.    
  19.     my_bit5(number);
  20.    
  21.     return 0;
  22. }
  23.  
  24.  
  25. /***
  26.  *  original_bit5.c
  27.  */
  28. #include "dummy_printf.h"   // get rid of stdio.h
  29. #include "original_bit5.h"
  30.  
  31. void original_bit5(int number) {
  32.     int mask = 1<<5;
  33.    
  34.     if ((number & mask) == 0)
  35.         dummy_printf("Bit is off");
  36.     else
  37.         dummy_printf("its on");
  38. }
  39.  
  40.  
  41. /***
  42.  *  my_bit5.c
  43.  */
  44. #include "dummy_printf.h"   // get rid of stdio.h
  45. #include "my_bit5.h"
  46.  
  47. void my_bit5(int number) {
  48.     dummy_printf((number & 0x20)?"its on":"Bit is off");   
  49. }
  50.  
  51.  
  52. /***
  53.  *  dummy_printf.c
  54.  */
  55. #include "dummy_printf.h"
  56.  
  57. void dummy_printf(const char* str) {
  58.     const volatile const char* port = (const char*)0x20000000;
  59.    
  60.     port = str;     // stop the compiler throwing anything away
  61. }
  62.  
  63.  
  64. /***
  65.  *  dummy_printf.h
  66.  */
  67. #ifndef _DUMMY_PRINTF_H_
  68. #define _DUMMY_PRINTF_H_
  69.  
  70. void dummy_printf(const char* str);
  71.  
  72. #endif
  73.  
  74.  
  75. /***
  76.  *  original_bit5.h
  77.  */
  78. #ifndef _ORIGINAL_BIT5_H_
  79. #define _ORIGINAL_BIT5_H_
  80.  
  81. extern void original_bit5(int number);
  82.  
  83. #endif
  84.  
  85.  
  86. /***
  87.  *  my_bit5.h
  88.  */
  89. #ifndef _MY_BIT5_H_
  90. #define _MY_BIT5_H_
  91.  
  92. extern void my_bit5(int number);
  93.  
  94. #endif
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement