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

Byte to Nibble

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



Joined: 20 Aug 2006
Posts: 12

View user's profile Send private message

Byte to Nibble
PostPosted: Sun Oct 05, 2008 4:08 pm     Reply with quote

Does anyone have a quick and efficient way of converting a byte into two separate nibbles in CCS? Years ago on a different platform, we used to be able to:
Code:

int8 a, MSnibble, LSnibble;
a = 174;                  // arbitrary number
MSnibble = a / 16;        // 'MSnibble' is the upper nibble
LSnibble = a mod 16;      // 'mod' for modulus
                          // 'LSnibble' is the lower nibble

However, this does not seem to work in CCS. A code snipet would be extremely helpful.

Boyd
PCM programmer



Joined: 06 Sep 2003
Posts: 21708

View user's profile Send private message

PostPosted: Sun Oct 05, 2008 4:21 pm     Reply with quote

Look at the lcd_send_byte() function in the CCS lcd.c driver.
It passes nibbles to another function and it shows how to do it.
BoydNielsen



Joined: 20 Aug 2006
Posts: 12

View user's profile Send private message

PostPosted: Sun Oct 05, 2008 4:33 pm     Reply with quote

Thanks. Sometimes one forgets the basics (shift right and bit masking)!
Code:

a = 174;
MSnibble = a >> 4;     // right shift to remove lower nibble
LSnibble = a & 0xF;    // bit mask to remove upper nibble

Boyd
Ttelmah
Guest







PostPosted: Mon Oct 06, 2008 3:12 am     Reply with quote

As a comment, the original would have worked, if you had used the C modulus function (%, not 'mod'). Shifts are much quicker though. However the compiler is 'smart' enough, to perform division by 16, using shifts, so it would not make too much difference. Smile

Best Wishes
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