home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / sys / amiga / programmer / 195 < prev    next >
Encoding:
Text File  |  1996-08-05  |  3.0 KB  |  94 lines

  1. Newsgroups: comp.sys.amiga.programmer
  2. Path: techfak.uni-bielefeld.de!isthesin
  3. From: isthesin@techfak.uni-bielefeld.de (Stephan Thesing)
  4. Subject: Re: Is this a SAS/C bug or have I coded it wrong?
  5. Message-ID: <DKns5B.449@hermes.hrz.uni-bielefeld.de>
  6. Sender: isthesin@TechFak.Uni-Bielefeld.DE (Stephan Thesing)
  7. Date: Thu, 4 Jan 1996 13:29:35 GMT
  8. References:  <4cfrc5$gsc@misery.millcomm.com>
  9. Nntp-Posting-Host: moos.techfak.uni-bielefeld.de
  10. Organization: Universitaet Bielefeld, Technische Fakultaet.
  11. X-Newsreader: xrn 8.01
  12.  
  13. In article <4cfrc5$gsc@misery.millcomm.com>, llucius@millcomm.com (Yambo) writes:
  14. |> Is this a SAS/C bug or am I getting more blind???
  15. |> 
  16. |> =========================================================================
  17. |> The following code:
  18. |> =========================================================================
  19. |> 
  20. |>    tmpbuf[10];
  21. |> 
  22. |>    int
  23. |>    test( void )
  24. |>    {
  25. |> 
  26. |>       if ( tmpbuf[0] & 0x1f == 1 )
  27. |>          return 1;
  28. |> 
  29. |>       return 0;
  30. |>    }
  31. |> 
  32.  
  33. No this is not a bug and yes, I am afraid you are getting more blind ;-)
  34.  
  35. The problem lies in the expression 'tmpbuf[0] & 0x1f == 1' which is
  36. equivalent to (according to the precedence rules of C):
  37.  'tmpbuf[0] & (0x1f == 1)' instead of (the intended)
  38.  '(tmpbuf[0] & 0x1f) ==1'
  39.  Since 0x1f!=1, the compiler can optimise this to 'tmpbuf[0] & 0', which is
  40.  always 0 and since tmpbuf[0] contains no side-effects, this is
  41.  optimised to 0.
  42.  
  43. In fact, one may argue that the precedence rules of C are somewhat broken,
  44. so my personal advice is:
  45.  PUT BRACKETS AROUND EVERYTHING, except that one can expect that 
  46.   addittion and multiplication have the usual precedences (and subtraction, division
  47.    too :-) 
  48.  
  49. Bye....
  50.     Stephan
  51.  
  52.  
  53.  
  54. |> =========================================================================
  55. |> Produces:
  56. |> =========================================================================
  57. |>                  SECTION      text,CODE
  58. |>    __code:
  59. |>    test:
  60. |>    ___test__1:
  61. |>                  MOVEQ.L        #$0,D0                   ;7000 
  62. |>    ___test__2:
  63. |>                  RTS                                     ;4e75 
  64. |>    __const:
  65. |>    __strings:
  66. |>                  XDEF           test
  67. |>    
  68. |>                  SECTION        __MERGED,BSS
  69. |>    __MERGEDBSS
  70. |>    tmpbuf:
  71. |>                  DS.B           40
  72. |>                  XDEF           tmpbuf
  73. |>                  END
  74. |> =========================================================================
  75. |> Is this right?  Where'd the "IF" go?  Do I have it coded wrong?
  76. |> 
  77. |> TIA
  78. |> 
  79. |> -- 
  80. |> __ Y_ a_ m_ b_ o_ | The leanest, meanest, fightinest sweet tater on Earth!
  81. |>    oo o  oo o  o  | 
  82. |>     o       o   o | llucius@millcomm.com
  83. |>  o oo    o     o  | 
  84. |> -- -- -- -- -- -- | http://www.millcomm.com/~llucius/index.html (coming soon)
  85. |> 
  86.  
  87. -- 
  88. ===============================================
  89. =             Stephan Thesing                 =
  90. =        AG Praktische Informatik             =
  91. =          Technische Fakult"at               =
  92. =         Universit"at Bielefeld              =
  93. ===============================================
  94.