Dec to Bin

Dec to Bin

十進位轉二進位 教學與筆記。

說明

計算完餘數迴圈後,下次進入迴圈計算餘數時 乘上10 進位

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
#include <stdio.h>

void main(){
int input = 13;

int mod = 0;
int final = 0;
int bit_loc = 1;

while(input){
mod = input % 2;
final = final + mod * bit_loc;
bit_loc = bit_loc * 10;
input = input / 2 ;
}

printf("%d\n", final); // 1101
}
Author

Meow Lucian

Posted on

2022-07-07

Updated on

2022-07-08

Licensed under

Comments