This repository has been archived by the owner on Jan 1, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
twosCompliment.c
81 lines (76 loc) · 2.03 KB
/
twosCompliment.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
/*
Date 08/10/2018
Author FredHappyface
*/
#include <stdio.h>
#include <math.h>
#include <string.h>
//Compiler version gcc 6.3.0
/*
Declare the twos_compliment and the binstr_to_int
functions
*/
int twos_compliment(char input[]);
int binstr_to_int(char input[], int offset_left);
/*
The main method tests the binstr_to_int function
and the twos_compliment function
*/
int main(void)
{
printf("The binary 10 as an integer is %d\n", binstr_to_int("10",0));
printf("The two's compliment of 1001 is %d\n", twos_compliment("1001"));
printf("The two's compliment of 0001 is %d\n", twos_compliment("0001"));
return 0;
}
/*
Method twos_compliment
Inputs string: input
Outputs int: output
This method takes a string representing a binary value
and returns an integer representation of the two's compliment
*/
int twos_compliment(char input[]){
// Find the value of the most significant bit
int sign_val = pow(2, strlen(input) - 1);
// Produce the ones compliment
char sone_compliment[] = "";
for(int index = 0; index < strlen(input); index ++ ){
if (input[index] == '1'){
strcat(sone_compliment, "0");
}
else{
strcat(sone_compliment, "1");
}
}
// Convert the one's compliment to an integer
int one_compliment = 0;
if(sone_compliment[0] - '0' == 0){
one_compliment = binstr_to_int(sone_compliment, 0);
}
else{
one_compliment = binstr_to_int(sone_compliment, 1);
one_compliment -= sign_val;
}
// Convert to two's compliment and return value
int output = one_compliment + 1;
return output;
}
/*
Method binstr_to_int
Inputs string: input, integer: offset_left
Outputs integer: output
This function takes a string representation of a binary value
an offset (this identifies the number of bits on the left to be
ignored) and returns an integer
*/
int binstr_to_int(char input[], int offset_left){
// Initialise variables
int output = 0;
int j = 0;
for(int i = strlen(input) -1; i >= offset_left; i--, j++){
// Increment output by the value of the character shifted by its position
output += (input [i] - '0') << j;
}
return output;
}