problem with long variable = 1<<31

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

I want to set the MSB of a long variable, but it gives me warning? Why? Do I have something wrong?

	unsigned long test;
	int main(void){
		test = (1<<31);
		while (1){
			test++;
		}
	}

gives the following warning:
../AVRGCC_studio_app.c:6: warning: left shift count >= width of type

Learn as long as you live

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

The type of "1" is an int, not a long.

test = (1UL << 31);

Regards,
Steve A.

The Board helps those that help themselves.

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

try test = (long)(1<<31);

next time for shure.

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

:) thanx. It works.

Learn as long as you live

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

Quote:
thanx. It works.

Which one worked? My guess is my answer since Leslie's does nothing different than your first attempt.

Regards,
Steve A.

The Board helps those that help themselves.

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

Both are worked. My problem was that always comming warning. That solved. Both ways not brings me any warning.

Learn as long as you live

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

kzdev wrote:
Both are worked. My problem was that always comming warning. That solved.
The "solution" by LeslieTroyer will give you a 0 as result.
But if you are only interested in the warning and not in the value of test, it works.

Stefan Ernst