-
Notifications
You must be signed in to change notification settings - Fork 7
Expand file tree
/
Copy pathprint2complbits.go
More file actions
127 lines (114 loc) · 2.31 KB
/
print2complbits.go
File metadata and controls
127 lines (114 loc) · 2.31 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
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
120
121
122
123
124
125
126
127
package main
import "os"
// import "fmt"
var exit bool
func ato(s string) int {
var n int
first := true
for i, _ := range s {
if (s[0] == '-' || s[0] == '+') && first {
first = false
continue
}
if s[i] > '9' || s[i] < '0' {
exit = true
}
n = n*10 + int(s[i]-'0')
}
if s[0] == '-' {
return -n
}
return n
}
func main() {
args := os.Args[1:]
if len(args) != 2 {
os.Stdout.WriteString("Usage: <program> <number> <width>\n")
return
}
n := ato(args[0])
for _, digit := range args[1] {
if digit > '9' || digit < '0' {
os.Stdout.WriteString("Incorrect width\n")
return
}
}
bi := ato(args[1])
var r string
if exit || n == 0 {
for i := 1; i <= bi; i++ {
os.Stdout.WriteString("0")
if i%4 == 0 {
os.Stdout.WriteString(" ")
}
}
os.Stdout.WriteString("\n")
return
}
sign := false
if n < 0 {
n = -n
sign = true
}
for n > 0 {
r = string(n%2+'0') + r
n /= 2
}
// fmt.Println(r)
for i := len(r); i < bi; i++ {
r = "0" + r
}
if sign {
sliceR := []rune(r)
for i := 0; i < len(sliceR); i++ {
if sliceR[i] == '0' {
sliceR[i] = '1'
} else {
sliceR[i] = '0'
}
}
for i := len(sliceR) - 1; i >= 0; i-- {
if sliceR[i] == '1' {
sliceR[i] = '0'
} else {
sliceR[i] = '1'
break
}
}
r = string(sliceR)
if sliceR[0] == '0' {
r = "1" + r
}
}
for i := 0; i < len(r); i++ {
os.Stdout.WriteString(string(r[i]))
if (i-3)%4 == 0 {
os.Stdout.WriteString(" ")
}
}
os.Stdout.WriteString("\n")
}
// edu
/**** usage
go run print2complbits.go 0 8
arg[0]-----> number
arg[1]-----> bit base
// when u give bit base out of range the program just ignore it
*****/
/********* Important
Notice that on limits like 128, 256 ... which is 1000 0000
we have also -128 which is also 1000 0000 this is why in
a 8 bits we can represent signed number only in the range
-128 to 127
********/
// https://www.allmath.com/twos-complement.php
/*Take a binary number 11111001 as an example. Without
TCR, we can interprate it as 2^7 + 2^6 + 2^5 + 2^4 +
2^3 + 1 = 249. With TCR (i.e. applying the mathematical
operation), we view it as -2^7 + 2^6 + 2^5 + 2^4 + 2^3 +
1 = -7.
Similiary, for another binary number, say 00000111.
Without TCR, we view it as 2^2 + 2^1 + 1 = 7. And with
TCR, we view it as 2^2 + 2^1 + 1 = 7, which is identical
to the former one.
*/