Byte shift by 16 does not work whereas 8 works

General discussion on mikroC for dsPIC30/33 and PIC24.
Post Reply
Author
Message
SSBISHT
Posts: 12
Joined: 11 Aug 2011 13:49

Byte shift by 16 does not work whereas 8 works

#1 Post by SSBISHT » 04 Jan 2013 19:22

Hi,

I am very puzzled, in my code " my_data |= (byte_1<<16);" does not shift byte but when I use code " my_data |= (1<<16);" This works fine, I have value of byte_1 = 1.

when shift by 8 both codes works fine
my_data |= (byte_1<<8); //works fine
my_data |= (1<<8); //works fine

Please make me understand what am I missing?
//----------------------------------------------
unsigned int long my_data=0;

void main()
{
while(1){
char byte_1 = 1; //0b00000001

my_data |= (byte_1<<16); // this shift does not work
// my_data |= (1<<16); // this shift does work

}
}
//-------------------------------------------------------
Best regads
Sunny

User avatar
dejan.odabasic
mikroElektronika team
Posts: 2649
Joined: 30 Apr 2012 14:20

Re: Byte shift by 16 does not work whereas 8 works

#2 Post by dejan.odabasic » 08 Jan 2013 16:28

Hello,

You have pushed the one "1" out of the scope of byte and then you are OR-ing it with my_data.
When you shift one as constant, you are using signed long int constant four byte in size.

Try shifting:

Code: Select all

my_data |= (byte_1<<5); // this shift does not work
my_data |= (1<<5);      // this shift does work
or something like this:

Code: Select all

my_data |= (-1<<16);
Best regards.

SSBISHT
Posts: 12
Joined: 11 Aug 2011 13:49

Re: Byte shift by 16 does not work whereas 8 works

#3 Post by SSBISHT » 09 Jan 2013 01:24

Hi dejan,

Thanks for your response but shift by 8 works fine.

Could you please explain me why?

when shift by 8 both codes works fine

my_data |= (byte_1<<8); //works fine
my_data |= (1<<8); //works fine

Best regards

Sunny

User avatar
dejan.odabasic
mikroElektronika team
Posts: 2649
Joined: 30 Apr 2012 14:20

Re: Byte shift by 16 does not work whereas 8 works

#4 Post by dejan.odabasic » 09 Jan 2013 12:22

Hello,

You are right, it's working, but it shouldn't and I'll report this behavior to our developers.
If you wish to avoid it, you can use:

Code: Select all

my_data |= (char)(byte_1<<8);
Best regards.

Post Reply

Return to “mikroC for dsPIC30/33 and PIC24 General”