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 CCS Technical Support

show bits in a printf???

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



Joined: 22 Nov 2006
Posts: 1

View user's profile Send private message

show bits in a printf???
PostPosted: Thu Dec 21, 2006 7:53 pm     Reply with quote

Hello

I�m trying to show bytes in the printf, is there some format that I can show the bites of a valor.
Like to show decimal is � %d � and hex is � %x� �.

I would be grateful if someone helps me.

Thanks,
Denin_online
jecottrell



Joined: 16 Jan 2005
Posts: 559
Location: Tucson, AZ

View user's profile Send private message

PostPosted: Thu Dec 21, 2006 7:57 pm     Reply with quote

You use both bits and bytes in your post, so I'm not sure if this will help. Here is a post describing how to print the bits in a byte.... binary....

http://www.ccsinfo.com/forum/viewtopic.php?t=25821&highlight=print+bit
Guest








PostPosted: Fri Dec 22, 2006 5:10 am     Reply with quote

here is Bob version its for 16bits but you can change it for 8bits easy

Code:

///////////////////////////////////////////////////////
/// coverts the final value to binary 16bits
void PrintAsBinary ( int16 value )
{   char display[16];
    int i;
    int binaryValue;
    for ( i = 0; i < LENGTH_OF_SHORT; i++ )
    {
        /* Check MSB against 1 */
        if ( 0x8000 == ( value & 0x8000 ) )
        {
            binaryValue = 1;
            display[i]='1';
        }
        else
        {
            binaryValue = 0;
             display[i]='x';
        }
        /* Print the binary value */
         printf(lcd_putc,"%c",display[i] );
        /* Shift next bit into mask position */
        value = value << 1;
    }
}
ckielstra



Joined: 18 Mar 2004
Posts: 3680
Location: The Netherlands

View user's profile Send private message

PostPosted: Fri Dec 22, 2006 10:04 am     Reply with quote

I like my version below as it is the most optimized version I've seen and it is very easy to adapt to other variable sizes (only requires changing the int16 to int8 or int32).

Code:
///////////////////////////////////////////////////////
// Prints the given value as binary.
void PrintAsBinary(int16 Value)
{
  int8 i;

  i = sizeof(Value) * 8;    // Get number of bits.
  do
  {
    // Check MSB against 1 and print the value.
    if (shift_right(&Value, sizeof(Value), 0) == 1)
      putc('1');
    else
      putc('0');
  } while (--i);
}
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