-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathexp-2-13.c
More file actions
55 lines (43 loc) · 1.08 KB
/
exp-2-13.c
File metadata and controls
55 lines (43 loc) · 1.08 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
/*************************************************************************
> File Name: exp-2-13.c
> Author: xiaoxiaoh
> Mail: xiaoxxhao@gmail.com
> Created Time: Thu Jun 29 16:04:19 2017
************************************************************************/
/*
* Write a C program to convert decimal to binary number system using bitwise operator.
*
* Example:
*
* Input any number: 22
*
* Output binary number: 00000000000000000000000000010110 (in 4 byte integer representation)
*
*/
#include <stdio.h>
#define INT_SIZE sizeof(int) * 8 // Integer size of bits
int main()
{
int num, index, i;
int bin[INT_SIZE];
/* Read a number from user */
printf("Enter a number: ");
scanf("%d", &num);
index = INT_SIZE;
while(index > 0)
{
index--;
//store 1 if LSB is set, otherwise 0
bin[index]= num &1;
//shift bit of num to one position right
num >>= 1;
}
/* print the converted binary */
printf("Coverted binary (in %lu type integer representation): ", sizeof(int));
for(i=0; i<INT_SIZE; i++)
{
printf("%d", bin[i]);
}
printf("\n");
return 0;
}