表示のバイナリ表現の数。[重複]
-
22-08-2019 - |
質問
まだ学習をCとって迷った:
番号をお渡ることはできないようなものは次のうちどれでしょう?
char a = 5;
printf("binary representation of a = %b",a);
> 101
やろうとしていただいて構いません、自分の方法の変容をバイナリー?
解決
あり(書く)、以下のようなもの完全な機能です。
#include <stdio.h> /* only needed for the printf() in main(). */
#include <string.h>
/* Create a string of binary digits based on the input value.
Input:
val: value to convert.
buff: buffer to write to must be >= sz+1 chars.
sz: size of buffer.
Returns address of string or NULL if not enough space provided.
*/
static char *binrep (unsigned int val, char *buff, int sz) {
char *pbuff = buff;
/* Must be able to store one character at least. */
if (sz < 1) return NULL;
/* Special case for zero to ensure some output. */
if (val == 0) {
*pbuff++ = '0';
*pbuff = '\0';
return buff;
}
/* Work from the end of the buffer back. */
pbuff += sz;
*pbuff-- = '\0';
/* For each bit (going backwards) store character. */
while (val != 0) {
if (sz-- == 0) return NULL;
*pbuff-- = ((val & 1) == 1) ? '1' : '0';
/* Get next bit. */
val >>= 1;
}
return pbuff+1;
}
追加するメインの終了までの操作
#define SZ 32
int main(int argc, char *argv[]) {
int i;
int n;
char buff[SZ+1];
/* Process all arguments, outputting their binary. */
for (i = 1; i < argc; i++) {
n = atoi (argv[i]);
printf("[%3d] %9d -> %s (from '%s')\n", i, n,
binrep(n,buff,SZ), argv[i]);
}
return 0;
}
しで実行する "progname 0 7 12 52 123"
を取得す:
[ 1] 0 -> 0 (from '0')
[ 2] 7 -> 111 (from '7')
[ 3] 12 -> 1100 (from '12')
[ 4] 52 -> 110100 (from '52')
[ 5] 123 -> 1111011 (from '123')
他のヒント
が無かったりすることもままあり方(を使用 printf
または別の標準ライブラリ関数)を印刷したりする事ができます。まっていく独自の機能です。
/* This code has an obvious bug and another non-obvious one :) */
void printbits(unsigned char v) {
for (; v; v >>= 1) putchar('0' + (v & 1));
}
使用している場合は、端末利用できる制御コードを印刷すバイトの自然順:
void printbits(unsigned char v) {
printf("%*s", (int)ceil(log2(v)) + 1, "");
for (; v; v >>= 1) printf("\x1b[2D%c",'0' + (v & 1));
}
に基づく dirkgentlyの回答, ものの、固定、二つのバグで、常に印刷固定桁数:
void printbits(unsigned char v) {
int i; // for C89 compatability
for(i = 7; i >= 0; i--) putchar('0' + ((v >> i) & 1));
}
#include<iostream>
#include<conio.h>
#include<stdlib.h>
using namespace std;
void displayBinary(int n)
{
char bistr[1000];
itoa(n,bistr,2); //2 means binary u can convert n upto base 36
printf("%s",bistr);
}
int main()
{
int n;
cin>>n;
displayBinary(n);
getch();
return 0;
}
利用のルックアップテーブルのように:
char *table[16] = {"0000", "0001", .... "1111"};
そしてそれぞれプリントnibbleこのような
printf("%s%s", table[a / 0x10], table[a % 0x10]);
確実に利用できるだけでテーブルで小幅の高速化と大きすぎます。
このコードを処理する必要がありますのニーズを最大64ビット.
char* pBinFill(long int x,char *so, char fillChar); // version with fill
char* pBin(long int x, char *so); // version without fill
#define width 64
char* pBin(long int x,char *so)
{
char s[width+1];
int i=width;
s[i--]=0x00; // terminate string
do
{ // fill in array from right to left
s[i--]=(x & 1) ? '1':'0'; // determine bit
x>>=1; // shift right 1 bit
} while( x > 0);
i++; // point to last valid character
sprintf(so,"%s",s+i); // stick it in the temp string string
return so;
}
char* pBinFill(long int x,char *so, char fillChar)
{ // fill in array from right to left
char s[width+1];
int i=width;
s[i--]=0x00; // terminate string
do
{
s[i--]=(x & 1) ? '1':'0';
x>>=1; // shift right 1 bit
} while( x > 0);
while(i>=0) s[i--]=fillChar; // fill with fillChar
sprintf(so,"%s",s);
return so;
}
void test()
{
char so[width+1]; // working buffer for pBin
long int val=1;
do
{
printf("%ld =\t\t%#lx =\t\t0b%s\n",val,val,pBinFill(val,so,0));
val*=11; // generate test data
} while (val < 100000000);
}
Output:
00000001 = 0x000001 = 0b00000000000000000000000000000001
00000011 = 0x00000b = 0b00000000000000000000000000001011
00000121 = 0x000079 = 0b00000000000000000000000001111001
00001331 = 0x000533 = 0b00000000000000000000010100110011
00014641 = 0x003931 = 0b00000000000000000011100100110001
00161051 = 0x02751b = 0b00000000000000100111010100011011
01771561 = 0x1b0829 = 0b00000000000110110000100000101001
19487171 = 0x12959c3 = 0b00000001001010010101100111000011
のが好きな自分で変容しています。みの小数点、六角レンチおよびoctal番号を支持形式の指示子.
が直接フォーマット指定子のためのC言語です。がんがあるのでそこで簡単なpythonスニペットをご理解いただくために、ステロールします。
#!/usr/bin/python
dec = input("Enter a decimal number to convert: ")
base = 2
solution = ""
while dec >= base:
solution = str(dec%base) + solution
dec = dec/base
if dec > 0:
solution = str(dec) + solution
print solution
説明:
dec=入力("と入力は十進数に変換す:") -迅速にユーザーのための数値入力がありなすCのような入力は、c言語ではscanf)
base=2 -を指定してベース2(バイナリ)
液="" -を空の文字列を連結弊社のソリューション
な月>=ベース: -当社の番号などが有名ですが、これらをベースに入
液=str(%)+ソ -の弾性率の数値をベースに追加してください最初の文字列にしてくれるものでなければなりませ番号の右用部門、残り法です。str()関数に変換し、結果の文字列になります。はできません連結整数文字列とpythonではなくタイプに変換します。
dec=dec/ベース -分割には十進数による基調整条件は次modulo
まdec>0: 液=str(月)+液 -ある場合は、追加の開始(1どちらかといえば)
印刷液 プリントの最終番号