정수를 2진수로 출력

(비트 출력에서 넘어옴)

1 개요[ | ]

비트 출력
정수를 비트로 출력
정수를 이진수로 출력
자연수를 이진수로 출력

2 C[ | ]

#include<stdio.h>
void printBit(int n) { 
    if(n>1) printBit(n>>1); 
    printf("%d", n&1); 
} 
int main() {
    for(int i=1; i<=100; i++) {
        printBit(i);
        printf("\n");
    }
}
#include<stdio.h>
void printBit(int num) {
    int q = num/2;
    int r = num%2;
    if( q == 0 && r == 0 ) return;
    printBit(q);
    printf("%d",r);
}
int main() {
    for(int i=1; i<=100; i++) {
        printBit(i);
        printf("\n");
    }
}
#include <stdio.h>
#define LENGTH 8
void printBit(int num) {
    int temp;
    for(int pos=LENGTH-1; pos>=0; pos--) {
        printf("%d", num>>pos&1);
    }
}
int main() {
    for(int i=1; i<=100; i++) {
        printBit(i);
        printf("\n");
    }
}

3 PHP[ | ]

for($i=1; $i<=100; $i++) {
    echo decbin($i). PHP_EOL;
}

4 Python[ | ]

d = 15
print( f'{d:b}' )
d = 15
print( '{0:b}'.format(d) )

5 같이 보기[ | ]

문서 댓글 ({{ doc_comments.length }})
{{ comment.name }} {{ comment.created | snstime }}