Shifting a long long where shift > sizeof(int)*CHAR_BIT

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

Shifting a long long does not behave as I would expect:

 

int main(void)
{
    DDRD = (1 << 4);
    unsigned long long int test = 1;
    test = (test << 16);
    if (test != 0)
    {
        PORTD = 1;
    }
    else
    {
        PORTD = 0;
    }
    while (1)
    {
        
    }
}

I would expect PORTD pin 4 to be high, but it its low. I am compiling with the default settings on Atmel Studio for the ATMEGA644P.

 

Is this a bug or do I need a compiler option?

 

Thanks,

j

This topic has a solution.

Last Edited: Wed. Feb 11, 2015 - 02:49 PM
This reply has been marked as the solution. 
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

> PORTD = 1

 

Are you sure this will set pin 4 of PORTD?

 

JW

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

As I remember 1 in the test needs to be 1UL

(but could be compiler dependent)

 

Edit should be 0

 

Edit a unsigned long long constant are ULL

Last Edited: Wed. Feb 11, 2015 - 03:12 PM
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Or there is always the chance my hastily written code is just a pile of trash... :) Thanks folks!

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

As I remember 1 in the test needs to be 1UL

(but could be compiler dependent)

 

Edit should be 0

 

Edit a unsigned long long constant are ULL

No, it does not need to be 0ULL, nor is it compiler dependent. Since "test" is an unsigned long long, the constant will be converted to an unsigned long long before the compare.

Regards,
Steve A.

The Board helps those that help themselves.

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

Steve wrote:
Since "test" is an unsigned long long, the constant will be converted to an unsigned long long before the compare.
... and, if there is a significant portion of code between the assignment and usage, it will be converted to an unsigned long long long before the compare ;-)

 

JW

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

wek3 wrote:

 

Steve wrote:

Since "test" is an unsigned long long, the constant will be converted to an unsigned long long before the compare.

... and, if there is a significant portion of code between the assignment and usage, it will be converted to an unsigned long long long before the compare ;-)

 

 

JW

If the compiler did >>not<< act that way, then you would long for the conversion long before the shift of the long long for a long way.

 

Longs:

Kyle, Howie, and Chris Long  http://ukendzone.com/2014/06/29/...

You can put lipstick on a pig, but it is still a pig.

I've never met a pig I didn't like, as long as you have some salt and pepper.