"정수를 2진수로 출력"의 두 판 사이의 차이

29번째 줄: 29번째 줄:
// 1100011
// 1100011
// 1100100
// 1100100
</source>
<source lang='c'>
#include <stdio.h>
#define LENGTH 8
void printBit(int num) {
    int temp;
    for(int pos=LENGTH-1; pos>=0; pos--) {
        temp = num >> pos;
        printf("%d", temp&1);
    }
}
int main() {
    for(int i=1; i<=100; i++) {
        printBit(i);
        printf("\n");
    }
}
// 00000001
// 00000010
// 00000011
// 00000100
// ...
// 01100010
// 01100011
// 01100100
</source>
</source>



2019년 1월 29일 (화) 01:29 판

1 개요

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

2 C

#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");
    }
}
// 1
// 10
// 11
// 100
// ...
// 1100010
// 1100011
// 1100100
#include <stdio.h>
#define LENGTH 8
void printBit(int num) {
    int temp;
    for(int pos=LENGTH-1; pos>=0; pos--) {
        temp = num >> pos;
        printf("%d", temp&1);
    }
}
int main() {
    for(int i=1; i<=100; i++) {
        printBit(i);
        printf("\n");
    }
}
// 00000001
// 00000010
// 00000011
// 00000100
// ...
// 01100010
// 01100011
// 01100100

3 PHP

<?php
for($i=1; $i<=100; $i++) {
    echo decbin($i). PHP_EOL;
}
# 1
# 10
# 11
# 100
# ...

4 같이 보기

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