CCS C Software and Maintenance Offers
FAQFAQ   FAQForum Help   FAQOfficial CCS Support   SearchSearch  RegisterRegister 

ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

CCS does not monitor this forum on a regular basis.

Please do not post bug reports on this forum. Send them to support@ccsinfo.com

Shift Left and Right Returning wrong Result.

 
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion
View previous topic :: View next topic  
Author Message
kufre



Joined: 19 Oct 2013
Posts: 7
Location: Nigeria

View user's profile Send private message

Shift Left and Right Returning wrong Result.
PostPosted: Mon Oct 21, 2013 8:57 am     Reply with quote

Hey Guys!

Pls can anyone help me out?

Am performing a bit-wise operation on data but I got a weird result.
Here is the code:
Code:

int8 testBitwise(void)
{
   unsigned int8 msb, lsb;
   unsigned int16 result;
   
   msb = 31;
   lsb = 48;
     
   result= (msb << 4) | (lsb >> 4);
   printf("%Lu", result);
   
}

On running the code, I got the "243" as the result.
But verifying the same values (for msb and lsb) on the windows' calculator (programmer's mode), I got "499" which is correct.

Pls could it be the variable type that is the problem or am using the bit-wise operator wrongly. I've not done this before.

I need your help!
Thanks
newguy



Joined: 24 Jun 2004
Posts: 1903

View user's profile Send private message

PostPosted: Mon Oct 21, 2013 9:02 am     Reply with quote

Code:
int8 testBitwise(void)
{
   unsigned int8 msb, lsb;
   unsigned int16 result;
   
   msb = 31;
   lsb = 48;
     
   result= ((unsigned int16)msb << 4) | ((unsigned int16)lsb >> 4);
   printf("%Lu", result);
   
}


You have to tell the compiler to treat your int8's like int16's.
kufre



Joined: 19 Oct 2013
Posts: 7
Location: Nigeria

View user's profile Send private message

Shift Left and Right Returning wrong Result.
PostPosted: Mon Oct 21, 2013 9:11 am     Reply with quote

Thanks newguy for your help. Its now working as expected.
Display posts from previous:   
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion All times are GMT - 6 Hours
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2005 phpBB Group