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

 
(사용자 2명의 중간 판 5개는 보이지 않습니다)
8번째 줄: 8번째 줄:
[[분류: C]]
[[분류: C]]
{{참고|C언어 비트 출력}}
{{참고|C언어 비트 출력}}
<source lang='c'>
<syntaxhighlight lang='c' run>
#include<stdio.h>
#include<stdio.h>
void printBit(int n) {  
void printBit(int n) {  
20번째 줄: 20번째 줄:
     }
     }
}  
}  
</source>
</syntaxhighlight>
<source lang='c'>
<syntaxhighlight lang='c' run>
#include<stdio.h>
#include<stdio.h>
void printBit(int num) {
void printBit(int num) {
36번째 줄: 36번째 줄:
     }
     }
}
}
// 1
</syntaxhighlight>
// 10
<syntaxhighlight lang='c' run>
// 11
// 100
// ...
// 1100010
// 1100011
// 1100100
</source>
<source lang='c'>
#include <stdio.h>
#include <stdio.h>
#define LENGTH 8
#define LENGTH 8
60번째 줄: 52번째 줄:
     }
     }
}
}
// 00000001
</syntaxhighlight>
// 00000010
// 00000011
// 00000100
// ...
// 01100010
// 01100011
// 01100100
</source>


==PHP==
==PHP==
[[분류: PHP]]
[[분류: PHP]]
<source lang='php'>
<syntaxhighlight lang='php' run>
<?php
for($i=1; $i<=100; $i++) {
for($i=1; $i<=100; $i++) {
     echo decbin($i). PHP_EOL;
     echo decbin($i). PHP_EOL;
}
}
# 1
</syntaxhighlight>
# 10
# 11
# 100
# ...
</source>


==Python==
==Python==
[[분류: Python]]
[[분류: Python]]
<source lang='Python'>
<syntaxhighlight lang='Python' run>
d = 10
d = 15
b = f'{d:b}'
print( f'{d:b}' )
print( b )
</syntaxhighlight>
# 1010
<syntaxhighlight lang='python' run>
print( type(b) )
d = 15
# <class 'str'>
print( '{0:b}'.format(d) )
</source>
</syntaxhighlight>
<source lang='python'>
d = 10
b = '{0:b}'.format(d)
print( b )
# 1010
print( type(b) )
# <class 'str'>
</source>
<source lang='Python'>
d = 10
b = bin(d)
print( b )
# 0b1010
print( type(b) )
# <class 'str'>
</source>


==같이 보기==
==같이 보기==

2021년 4월 14일 (수) 01:39 기준 최신판

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 }}