c_cpp 在C中从右侧打印位

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了c_cpp 在C中从右侧打印位相关的知识,希望对你有一定的参考价值。

#include <stdio.h>
#include <limits.h>
// print right most bits in c

// prints an nth bit from the right
void print_bit_from_right(unsigned num, unsigned places)
{
   putchar(((num >> places) & 1) ? '1' : '0');
}

void print_all_bits_right(unsigned num)
{
  unsigned places = sizeof(unsigned) * CHAR_BIT;
  while(--places) putchar(((num >> places) & 1) ? '1' : '0');
  putchar('\n');
}


int main(void) {
  print_all_bits_right(1000);
  // 0000000000000000000000111110100
  return 0;
}

以上是关于c_cpp 在C中从右侧打印位的主要内容,如果未能解决你的问题,请参考以下文章