next up previous contents index
Next: 8.1.12 Peephole Optimizer Up: 8.1 Optimizations Previous: 8.1.10 Nibble and Byte   Contents   Index


8.1.11 Highest Order Bit

It is frequently required to obtain the highest order bit of an integral type (long, int, short or char types). SDCC recognizes the following expression to yield the highest order bit and generates optimized code for it, e.g.:

unsigned int gint;  
 
foo () {  
  unsigned char hob;  
  ...  
  hob = (gint >> 15) & 1;  
  ..  
}
will generate the following code:

                          61 ;  hob.c 7  
000A E5*01                62         mov   a,(_gint + 1)  
000C 23                   63         rl    a  
000D 54 01                64         anl   a,#0x01  
000F F5*02                65         mov   _foo_hob_1_1,a
Variations of this case however will not be recognized. It is a standard C expression, so I heartily recommend this be the only way to get the highest order bit, (it is portable). Of course it will be recognized even if it is embedded in other expressions, e.g.:

xyz = gint + ((gint >> 15) & 1);
will still be recognized.


next up previous contents index
Next: 8.1.12 Peephole Optimizer Up: 8.1 Optimizations Previous: 8.1.10 Nibble and Byte   Contents   Index
Erik Petrich 2005-09-07