2011-07-06 14:59:37     Bit shift problem

Document created by Aaronwu Employee on Oct 16, 2013
Version 1Show Document
  • View in full screen mode

2011-07-06 14:59:37     Bit shift problem

Wojtek Skulski (UNITED STATES)

Message: 102172   




not sure if the following problem is related to the toolchain. Let me describe it here. Disclaimer: I am using the 2009 distro because of the network driver. So maybe the problem was already fixed.


I defined bit 31 in the FPGA register to test whether the readout is allowed. In my readout software I am testing this bit. Surprisingly to me, method #1 does not work while method #2 does. I am reporting it here.


  /*waveform capture still running ==> do not acquire waveform*/

  virt_addr = (long *) FPGA_ODD_THINGS_REG;

  value = * virt_addr;


  bit = (value >> 32) & bmBit0;   /* method #1 does not work */


  hi = (short) ((value & 0xFFFF0000) >> 16);  /* method #2 works*/

  bit = (hi >> 16) & bmBit0;

  if ( bit != 0) {

      Error (FPGA_NOT_STOPPED, form_method, getvars, postvars, info);




The "does not work" means that despite the bit being set (I have an independent means of looking at it) the Error is not called when I use method #1.


The variables are declared as follows:


#define bmBit0  1


  long *virt_addr;      /* mem pointer*/

  long value;           /* 32-bit read result.*/

  short lo, hi;         /* low and high parts of 32-bit word  */

  int bit;              /* aux variable for bit testing  */




2011-07-06 15:07:04     Re: Bit shift problem

Mike Frysinger (UNITED STATES)

Message: 102173   


pretty sure both examples are wrong.  the short cast probably sign extends the result and makes you think it is working.


shifting a 32bit value by 32bits doesnt really make sense.  pretty sure you want to shift by 31 bits.




2011-07-06 15:24:58     Re: Bit shift problem

Wojtek Skulski (UNITED STATES)

Message: 102174   




you are right.. ditto for shifting a 16-bit variable by 16 in method #2. Meanwhile I noticed there was a warning issued by the compiler for the line with "method #1".


warning: right shift count >= width of type


It makes me realize that bit was declared as int, which most likely is 16 bit. There must have been an interesting combination of type conversion and the sign extension performed by the compiler in both methods.


So I changed bit to long and shifted by 31. It now works...


Programming *is* interesting.




2011-07-06 15:31:20     Re: Bit shift problem

Mike Frysinger (UNITED STATES)

Message: 102175   


"int" is 32bit on Blackfin systems.  "short" is 16bit.


you can check by simply doing:

printf("%i\n", sizeof(int));




2011-07-06 15:53:30     Re: Bit shift problem

Wojtek Skulski (UNITED STATES)

Message: 102176   




I stand corrected. So it must have been sign extension. My C Quick Reference does not say anything in this respect about the ">>" operator. It says "move bits". It probably depends on the signed/unsigned type whether its is an arithmetic or logical shift. I did not think of this because in other languages which I am also using these two types of shift are coded in two different ways.