-
Notifications
You must be signed in to change notification settings - Fork 0
/
decimal_to_binary_conoversion.js
113 lines (95 loc) · 3.11 KB
/
decimal_to_binary_conoversion.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
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
/**
* The problem statement is to create a function that
* converts a number to its array and make's 2's
* compliment of it .
*/
/**
* Function to convert number to binary
* @param {Number} number
* @param {Number} numLength
* @returns {Array}
*/
function numToBinary(number,numLength) {
let bin = [] //array to store the binary form of the number
while(number>0){
// pushing the remainer part when divided by 2
bin.push(number%2)
// changing the original number to integer division by 2
number = Math.floor(number/2)
}
// If the length of the binary representation is less than the given length
// pushing exciding 0s to match
// the required length of the binary representation
if(bin.length < numLength){
for(let i =0;i<=numLength-bin.length;i++){
bin.push(0);
}
}
return bin.reverse()
}
/**
* @param {Number} number - the input number
* @param {Number} numLength - length of the number in bits.
* @returns {Array} Array - An array that is the 2's compliment of input numeber
*/
function getSimple2sCompliment(number,numLength){
if(parseInt(numLength)>52){
throw new Error("The given bit length exceeds the limit")
return 0
}
let sign = 0;
let binaryNum = [];
//Find out if the number is positive or negative
let dectobin
if(number < 0){
/**
* if the number is negative add teh sign first into the answer
* subtract one form the absolute value of the number and then
* convert it to binary
*/
sign = 1
binaryNum.push(sign)
dectobin = numToBinary(Math.abs(number)-1,numLength+2)
}else{
binaryNum.push(sign)
dectobin = numToBinary(number,numLength+2)
}
//pushing the binary representation of the number to the array to be returned
for(let i=0;i<dectobin.length;i++){
binaryNum.push(dectobin[i])
}
return binaryNum
}
/**
* Function to convert a binary code to decimal
* @param {String} binary - The binary string you want to convert
* @returns {Number} a negative or positive number representing the inputted binary
*/
function getSimplDecimalFrom2sCompliment(binary){
let num =0
let sign = 1 // variable to keep an account of the sign of the number
//to check if the nunber is negative
if(binary[0]=="1"){
sign = -1
}
let j =0 //varible that will store the power to which 2 will be raised
//rasiing each number to the weight of their position and adding them to
// find the number
for(let i=binary.length-1;i>0;i--){
num += (2**j)*parseInt(binary[i])
j++
}
return num*sign
}
/**
* Function for testing the created functions created above
*/
function testing(){
console.log(getSimple2sCompliment(9,"16"))
console.log(getSimple2sCompliment(83,12))
console.log(getSimple2sCompliment(-83,8))
console.log(getSimplDecimalFrom2sCompliment("110100010"))
console.log(getSimplDecimalFrom2sCompliment("11011010"))
console.log(getSimplDecimalFrom2sCompliment("01100010"))
}
testing()