warning: left shift count >= width of type

Go To Last Post
9 posts / 0 new
Author
Message
#1
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Hi everyone.

Just wondering if the following is a bug in AVR Studio 4 or if the problem is in my own 'firmware'.

The following code results in the specified warning at compile-time:

unsigned long a;
a=_BV(31);

The same error is obtained if the argument of the bitshift operator is anything over 15. I don't understand this 'cos the unsigned long type is supposed to be 32 bits.

Thanks for reading!

=TN=

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

If this is WINAVR (GCC) try:

uint32_t a;

just to see if this works or not.

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Quote:
If this is WINAVR (GCC) try:

uint32_t a;

just to see if this works or not.

Nope. _BV(x) is short for (1 << x). In this, "1" is a 16 bit number, therefore shifting it more than 16 is illogical. Making the variable that the value is used with will do nothing. You have to ensure that "1" is a long by using "1UL".

Regards,
Steve A.

The Board helps those that help themselves.

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Steve, replacing _BV(31) with 1UL<<31 does work so thanks for that.

I guess this is a limitation in the way the _BV(x) macro is implemented. Arguable whether it's a bug or not!

It's one of those rare things where once the solution is known it's not all that satisfying. Why isn't the standard practice of upgrading the lower spec variable to match the higher one adhered to? In any case, enough semi-coherent ranting from me. Thanks for providing the solution.

=TN=
also Vancouver, BC

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

No it's not a bug. The compiler designers just set the max. shift to be 16 bits.

1) Studio 4.18 build 716 (SP3)
2) WinAvr 20100110
3) PN, all on Doze XP... For Now
A) Avr Dragon ver. 1
B) Avr MKII ISP, 2009 model
C) MKII JTAGICE ver. 1

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Quote:

The compiler designers just set the max. shift to be 16 bits.

Eh? This is a question as to whether the LibC authors should have used:

#define _BV(x) (1UL<<x)

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Quote:
The compiler designers just set the max. shift to be 16 bits.
No, the C language did. Literal constants without qualifiers are the size of an int, and on the AVR, an int is 16 bits.

Regards,
Steve A.

The Board helps those that help themselves.

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

My bad, yeah it's a Library issue, sorry.

1) Studio 4.18 build 716 (SP3)
2) WinAvr 20100110
3) PN, all on Doze XP... For Now
A) Avr Dragon ver. 1
B) Avr MKII ISP, 2009 model
C) MKII JTAGICE ver. 1