-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbitit.c
120 lines (88 loc) · 1.87 KB
/
bitit.c
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
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
#include <stdio.h>
#include <limits.h>
#include "bitit.h"
/*
- long ei toimi?
- WETO valittaa -1:stä
*/
int main(int argc, char *argv[]) {
printf("System CHAR_BIT: %d \n", CHAR_BIT);
scharBitit(-33);
shortBitit(2015);
intBitit(2015);
longBitit(2015);
return 0;
}
void scharBitit(signed char x) {
unsigned int pos = CHAR_BIT;
unsigned short num_zeros = 0;
unsigned short num_ones = 0;
/*printf("%d\n", x);
*/
for(pos; pos>=1; pos--) {
/*printf("pos: %d \n", pos);
*/
if((x>>pos-1) & 1) {
printf("1");
num_ones++;
} else {
printf("0");
num_zeros++;
}
}
printf("\n%d\n%d\n\n", num_zeros, num_ones);
}
void shortBitit(short int x) {
unsigned int pos = CHAR_BIT*sizeof(short);
unsigned short num_zeros = 0;
unsigned short num_ones = 0;
printf("%d\n", x);
for(pos; pos>=1; pos--) {
/*printf("pos: %d \n", pos);
*/
if((x>>pos-1) & 1) {
printf("1");
num_ones++;
} else {
printf("0");
num_zeros++;
}
}
printf("\n%d\n%d\n\n", num_zeros, num_ones);
}
void intBitit(int x) {
unsigned int pos = CHAR_BIT*sizeof(int);
unsigned short num_zeros = 0;
unsigned short num_ones = 0;
printf("%d\n", x);
for(pos; pos>=1; pos--) {
/*printf("pos: %d \n", pos);
*/
if((x>>pos-1) & 1) {
printf("1");
num_ones++;
} else {
printf("0");
num_zeros++;
}
}
printf("\n%d\n%d\n\n", num_zeros, num_ones);
}
void longBitit(long int x) {
unsigned int pos = CHAR_BIT*sizeof(long int);
unsigned short num_zeros = 0;
unsigned short num_ones = 0;
printf("%d\n", x);
for(pos; pos>=1; pos--) {
/*printf("pos: %d \n", pos);
*/
if((x>>pos-1) & 1) {
printf("1");
num_ones++;
} else {
printf("0");
num_zeros++;
}
}
printf("\n%d\n%d\n\n", num_zeros, num_ones);
}