简体   繁体   English

输出 unsigned short (C) 的位值

[英]Outputting bit values for unsigned short (C)

I have a function that takes an unsigned short and displays the bit values for it.我有一个函数,它采用无符号短整型并显示它的位值。 Currently it displays all but the leading 0's, and I would like to display all 16 bit places instead.目前它显示除前导 0 之外的所有内容,我想显示所有 16 位位置。

void display_result(unsigned short result)
{
     if(result >> 1)
     {
          display_result(result >> 1);
     }
     if(result & 1)
     {
          putc('1', stdout);
     }
     else
     {
          putc('0', stdout);
     }
     return;
}

Example input: 3示例输入:3
Desired output: 0000000000000011期望输出:00000000000000011
Actual output: 11实际输出:11

My understanding of my code was that it essentially iterated through the bit values of result the way it would through an array, outputting based on the if statement, but that's not what actually seems to happen, which tells me that I'm missing something.我对我的代码的理解是,它基本上以遍历数组的方式遍历 result 的位值,根据 if 语句输出,但这并不是实际发生的情况,这告诉我我遗漏了一些东西。 Can someone throw some light on this?有人可以对此有所了解吗? (Note: I have searched for this info before posting, but my search fu may be lacking). (注意:我在发帖之前已经搜索过此信息,但我的搜索功能可能缺乏)。

You need a full 16 iterations, not just until there are no significant digits.您需要完整的 16 次迭代,而不仅仅是直到没有有效数字。 Using your approach, you could add a depth parameter:使用您的方法,您可以添加一个深度参数:

#include <stdio.h>
#include <limits.h>
    static void display_result_n_(unsigned short result, int n)
    {
         if(--n) { display_result_n_(result>>1,n); }
         if(result & 1) { putc('1', stdout); }
         else { putc('0', stdout); }
         return;
    }

void display_result(unsigned short result)
{
    display_result_n_(result,sizeof(unsigned short)*CHAR_BIT);
}
int main()
{
    display_result(3);
    puts("");
}

Or you could do it iteratively.或者你可以反复进行。 For bases-of-two radixes, you don't need reversions (done for you by the recursion) if you simply test from the other (most significant) side by sliding a 1<<15 mask 16 times.对于基于两个基数的基数,如果您只是通过滑动1<<15掩码 16 次从另一侧(最重要)进行测试,则不需要反转(通过递归为您完成)。

void display_result(unsigned short result)
{
    unsigned mask = 1U<<((sizeof(unsigned short))*CHAR_BIT-1);
    for(;mask;mask>>=1) putc('0'+ !!(result&mask),stdout );
}

The recursive function needs to know how many times it must be recursively called.递归函数需要知道它必须被递归调用多少次。 That is it needs somehow to know the size of an object of the type unsigned short.也就是说,它需要以某种方式知道 unsigned short 类型的对象的大小。

In general there are two approaches.一般有两种方法。

The first one is two write two functions.第一个是两个写两个函数。 The first one will have at least one parameter, the number, and the second one will have at least two parameters, the number and the current bit position.第一个将至少有一个参数,即数字,第二个将至少有两个参数,即数字和当前位位置。

Here is a demonstrative program.这是一个演示程序。

#include <stdio.h>
#include <limits.h>

FILE * display_result( unsigned short n, size_t size, FILE *fp )
{
    if ( size != 0 )
    {
        display_result( n >> 1, size - 1, fp );
        fputc( ( n & 1 ) + '0', fp );
    }
    
    return fp;
}

FILE * bit_image( unsigned short n, FILE *fp )
{
    display_result( n, sizeof( unsigned short ) * CHAR_BIT, fp );
    
    return fp;
}

int main(void) 
{
    for ( unsigned short i = 0; i < 16; i++ )
    {
        fputc( '\n', bit_image( i, stdout ) );
    }
    
    return 0;
}

The program output is程序输出是

0000000000000000
0000000000000001
0000000000000010
0000000000000011
0000000000000100
0000000000000101
0000000000000110
0000000000000111
0000000000001000
0000000000001001
0000000000001010
0000000000001011
0000000000001100
0000000000001101
0000000000001110
0000000000001111

The second approach is to have a static local variable that will count recursive calls.第二种方法是使用静态局部变量来计算递归调用。

For example例如

#include <stdio.h>
#include <limits.h>

FILE * display_result( unsigned short n, FILE *fp )
{
    static size_t size;
    
    if ( size != CHAR_BIT * sizeof( unsigned short ) )
    {
        ++size;
        display_result( n >> 1, fp );
        fputc( ( n & 1 ) + '0', fp );
        --size;
    }
    
    return fp;
}

int main(void) 
{
    for ( unsigned short i = 0; i < 16; i++ )
    {
        fputc( '\n', display_result( i, stdout ) );
    }
    
    return 0;
}

The program output is the same as shown above.程序输出与上图相同。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM