-
Notifications
You must be signed in to change notification settings - Fork 0
/
operations.js
67 lines (60 loc) · 1.79 KB
/
operations.js
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
/*
These are the basic ALU operations on bit sequences (as strings)
*/
let boolToBinChar = bool => bool ? '1' : '0'
export const addTwoComp = (xStr, yStr, truncateTo = false)=>{
let x = xStr.split(``).reverse() // LSB first
let y = yStr.split(``).reverse()
let results = []
let carry = 0
while (x.length > 0 || y.length > 0 || carry){
let xBit = Number(x[0]) || 0 // A missing bit from just one party will turn into 0
let yBit = Number(y[0]) || 0
let sum = xBit + yBit + carry
carry = 0
let remainder = 0
if (sum === 3){
carry = 1
remainder = 1
} else if (sum === 2){
carry = 1
} else if (sum === 1){
remainder = 1
}
results.push(remainder)
x = x.slice(1)
y = y.slice(1)
}
if (truncateTo) results = results.slice(0, truncateTo) // Cuts off most significant bits (still reversed)
return results.reverse().join(``)
}
export const addFloating = ()=>{
throw Error('unimplemented')
// TODO
}
export const boolOr = (x, y) => {
let longer = y.length > x.length ? y : x
let resultArr = [...longer].map((val, i) => Number(x[i]) || Number(y[i]))
return resultArr.join(``)
}
export const boolAnd = (x, y) => {
let longer = y.length > x.length ? y : x
let resultArr = [...longer].map((val, i) => Number(x[i]) && Number(y[i]))
return resultArr.join(``)
}
export const boolXor = (x, y) => {
let longer = y.length > x.length ? y : x
let resultArr = [...longer].map((val, i) =>{
let xBit = Number(x[i]),
yBit = Number(y[i])
let booled = (xBit || yBit) && !(xBit && yBit)
return boolToBinChar(booled)
})
return resultArr.join(``)
}
export const bitRotateRight = (bits, places) => {
let moduloed = places % bits.length
let cutPoint = bits.length - moduloed
let charactersOffRightEdge = bits.substr(cutPoint)
return charactersOffRightEdge + bits.substr(0, cutPoint)
}