我很困惑我应该在printcharasbinary()函数中改变什么来创建printintasbinary()函数时用于整数。 [英] I am confused on what I should change in the printcharasbinary() function to work for integers when creating the printintasbinary() function.

查看:64
本文介绍了我很困惑我应该在printcharasbinary()函数中改变什么来创建printintasbinary()函数时用于整数。的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Hello我在binaryformat.c文件中给出了一个函数printCharAsBinary(unsigned char number)。我必须创建第二个函数printIntAsBinary(unsigned int number)来处理整数。但是我对printCharAsBinary()函数在创建printIntAsBinary()函数时应该更改为什么感到困惑。



带有printCharAsBinary函数的Binaryformat.c 。

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.

#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 
   }
}





我的尝试:



我不知道如何尝试这个问题。我在binaryformat.c文件中给出了一个函数printCharAsBinary(unsigned char number)。我必须创建第二个函数printIntAsBinary(unsigned int number)来处理整数。但是我对printCharAsBinary()函数在创建printIntAsBinary()函数时应该为整数工作的内容感到困惑。



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.

推荐答案

这是一种实现的方法以二进制形式显示一个字节:



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

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 :

typedef union
{
    uchar  bytes[8];
    int    intvalue;
    short  shortvalue;
    float  floatvalue;
    double doublevalue;
} ByteUnion;



如果将intvalue设置为整数值,则可以将单个字节传递给print函数以显示它们。这是一个函数,它将打印尽可能多的字节和使用它的样本。




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.


在所有位都在内存之前回答,但你必须正确解释它们。因此,内存缓冲区的重复将完成这项工作。

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.
char buffer[4];
memcpy( buffer, &uiMemory, 4 );// make copy of the bits for clarity  

for( int i = 0; i < 4; i++ ) {
 printCharAsBinary( buffer[i]);
}

你也可以做花式指针 - 因为我喜欢C ++

you also can do fancy pointer stuff - for that reason I love C++

char *p = (char*) &uiMemory;//assign address of the ui to char* p

for( int i = 0; i < 4; i++ ) {
 printCharAsBinary( p[i]);
}


这篇关于我很困惑我应该在printcharasbinary()函数中改变什么来创建printintasbinary()函数时用于整数。的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆