Click here to Skip to main content
15,895,011 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hello I am given a function printCharAsBinary(unsigned char number) in the file binaryformat.c. I have to create a second function printIntAsBinary(unsigned int number) to work with integers. However I am confused on what I should change in the printCharAsBinary() function to work for integers when creating the printIntAsBinary() function.

Binaryformat.c with the printCharAsBinary function.
C
#include <xc.h>         // library of functions for this chip 
#include <stdio.h>      // library containing printf() function


void printIntAsBinary(unsigned int number);
void WaitOneSecond(void);

int main(void)  
{
    unsigned i = 0;
    configureUSART(9600ul, 1); // configure MCU serial communication module to run at 9600 baud 
                               // defined in configureUSART.c

    WaitOneSecond();   // The splash screen lasts about one second
                       // LCD will not respond to printf() until it is finished.



    for(i=0; i < 256; i++)
      {
	printf("\n\r %u = ",i);
        printIntAsBinary((unsigned int)i);
        
      }                         

while(1)
   {
	  // MCUs run continuously so an endless loop is required.
   }
}	


void printIntAsBinary(unsigned int number)
{

if ( ((number & 0b10000000) >> 7 ) == 1)
   printf("0b1");
else
   printf("0b0");

if ( ((number & 0b01000000) >> 6 ) == 1)
   printf("1");
else
   printf("0");


if ( ((number & 0b00100000) >> 5 ) == 1)
   printf("1");
else
   printf("0");


if ( ((number & 0b00010000) >> 4 ) == 1)
   printf("1");
else
   printf("0"); 


if ( ((number & 0b00001000) >> 3 ) == 1)
   printf("1");
else
   printf("0");


if ( ((number & 0b00000100) >> 2 ) == 1)
   printf("1");
else
   printf("0");

if ( ((number & 0b00000010) >> 1 ) == 1)
   printf("1");
else
   printf("0");


if ( ((number & 0b00000001) ) == 1)
   printf("1");
else
   printf("0");

}


void WaitOneSecond(void)
{
int  i = 0;
for(i=0; i<=5; i++) 
   {
    _delay(50000ul);  // 50 000 * 4 / 1 000 000 = 1/5 s 
   }
}


What I have tried:

I am not sure on how to attempt the problem.I am given a function printCharAsBinary(unsigned char number) in the file binaryformat.c. I have to create a second function printIntAsBinary(unsigned int number) to work with integers. However I am confused on what I should change in the printCharAsBinary() function to work for integers when creating the printIntAsBinary() function.
Posted
Updated 22-Jan-18 13:05pm
v2
Comments
Dotnet_Dotnet 22-Jan-18 1:57am    
sir where you defines printintasbinary
Member 13635589 22-Jan-18 12:31pm    
Hi This is the printCharAsBinary function. I have to still create the printIntAsBinary function. The question states that what will I changer on the printCharAsBinary fnction to work with integers?
Thanks,
I am very confused
CPallini 22-Jan-18 3:23am    
You didn't show us printCharAsBinary definition.
Member 13635589 22-Jan-18 12:30pm    
Hi This is the printCharAsBinary function. I have to still create the printIntAsBinary function. The question states that what will I changer on the printCharAsBinary fnction to work with integers?
Thanks,
I am very confused
Richard MacCutchan 22-Jan-18 4:25am    
You should learn how to create loops to do repeats of the same instructions.

You will always send 0b first,
then just use a counter for the bit shift and repeat the print of 0 or 1 for each bit.

Here is one way to implement displaying a byte in binary form :

C++
typedef unsigned char uchar;

void PrintByteInBinary( uchar byte )
{
    uchar mask = 0x080;  // set initially to most significant bit
    int n;
    for( n = 0; n < 8; ++n )
    {
        printf( "%s", byte & mask ? "1" : "0" );
        mask >>= 1;
    }
}

You can pass in the individual bytes of any type and size of data. Here is a way to use a union so you can easily extract the bytes :
C++
typedef union
{
    uchar  bytes[8];
    int    intvalue;
    short  shortvalue;
    float  floatvalue;
    double doublevalue;
} ByteUnion;

If you set the intvalue to an integer value then you can pass the individual bytes to the print function to display them. Here's a function that will print as many bytes as you have and a sample of using it.

void PrintBytesInBinary( uchar *bytes, int byteCount )
{
    int n;
    for( n = 0; n < byteCount; ++n )
         PrintByteInBinary( bytes[n] );
}

void PrintValues( void )
{
   ByteUnion bu = { 0 };

   bu.intvalue = 42;
   PrintBytesInBinary( bu.bytes, sizeof( int ) );

   bu.shortvalue = 96;
   PrintBytesInBinary( bu.bytes, sizeof( short ) );

   bu.floatvalue = 3.1415f;
   PrintBytesInBinary( bu.bytes, sizeof( float ) );

   bu.doublevalue = 1.4142;
   PrintBytesInBinary( bu.bytes, sizeof( double ) );
}
I have not tested this code but it should be close to functional. Good luck.
 
Share this answer
 
v2
Comments
Rick York 22-Jan-18 21:25pm    
Of course, using the union is not necessary. One could just pass the address of the value and use a cast to make it the appropriate type. That is a bit simpler really.
As answered before all bits are in memory but you must interpret them correctly. So an repeat on the memory buffer will do the job.
C++
char buffer[4];
memcpy( buffer, &uiMemory, 4 );// make copy of the bits for clarity  

for( int i = 0; i < 4; i++ ) {
 printCharAsBinary( buffer[i]);
}
you also can do fancy pointer stuff - for that reason I love C++
C++
char *p = (char*) &uiMemory;//assign address of the ui to char* p

for( int i = 0; i < 4; i++ ) {
 printCharAsBinary( p[i]);
}
 
Share this answer
 
Comments
Member 13635589 22-Jan-18 13:24pm    
Hi This is the printCharAsBinary function. I have to still create the printIntAsBinary function. The question states that what will I change on the printCharAsBinary fnction to work with integers?
Thanks,
I am very confused
Member 13635589 22-Jan-18 13:25pm    
I do not mean to cheat or anything as this is for homework practice only. If you could please show me how to do this, I would greatly appreciate it.
Thanks
KarstenK 23-Jan-18 8:47am    
That is NOT a cheat, but C/C++ as it meant to be deployed. A nice tutorial about bit shifting: https://developer.apple.com/library/content/documentation/Swift/Conceptual/Swift_Programming_Language/AdvancedOperators.html#//apple_ref/doc/uid/TP40014097-CH27-ID28

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900