C中的二进制转换 [英] Binary conversions in C

查看:99
本文介绍了C中的二进制转换的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

#include <xc.h>         // library of functions for this chip 
#include <stdio.h>      // library containing printf() function
#include "configureUSART.h" // library for configureUSART(baud)
#include "configuration_bits.h"


void printCharAsBinary(unsigned char 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);
        printCharAsBinary((unsigned char)i);
      }                         

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

void printCharAsBinary(unsigned char 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 
   }
}





我的尝试:



我应该创建一个int的二进制转换代码,我有以下代码用于char。我知道char是8位,int是16位,我需要更多的移位操作来检查有两个有多少幂



如果我有7我可以在这里找到14.有人可以向我解释这些班次运营商是如何建立的。我知道他们正在检查两个人的力量,但他们是如何做到的,如果是,我怎么能将它应用于更高的位。我如何设置移位运算符?



使用sparkfun和PIC18F4525的serLCD



What I have tried:

I am supposed to create a code for binary conversion of int, I have the following code for char. I understand that char is 8 bits and int is 16 bits and I would need more shift operation to check " how many powers of two are there"

if I have 7 here would I have 14. could someone explain to me how these shift operators are set up. I understand they are checking the power of two but how are they doing it and if yes how can I apply this to higher bits. how do i set up shift operators?

Using serLCD by sparkfun and PIC18F4525

推荐答案

你必须明白所谓的位移运算符(>>)将输入的位移动到下侧。因此,重复调用将所有位移出存储器。



此运算符移动内存的位,将其解释为某个变量。要真正理解它,您必须了解所有位都在内存中,实际值是该内存的解释。用字符串最容易理解。一些示例代码来自 Microsoft



我在堆栈溢出。结果很好地存储在字符串缓冲区中。
You must understand the so called bit shift operator (>>) which moves the bits of the input to lower side. So a repeating call moves all bits out of the memory.

This operator moves the bits of the memory which is interpreted as some variable. To really understand it you must understand that all bits are in the memory and the actual value is the interpretation of that memory. Easiest to understand with strings. Some explaation with some sample code from Microsoft.

I found a really nice implementation of your task on stack overflow. The results is nicely stored in string buffers.


我已经解决了这个谢谢大家的帮助
I have solved this thank you all for help


这篇关于C中的二进制转换的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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