Write three C programs for the questions below: binary number defined as: int bi
ID: 3589202 • Letter: W
Question
Write three C programs for the questions below:
binary number defined as: int binNum[8];
1. Defining sum, bin1, and bin2 as binary numbers as above, write the void function
void binaryAdd(int sum[], int *cBit,
const int bin1[], const int bin2[])
to compute sum as the sum of the two binary numbers, bin1 and bin2. cBit should be the value of the carry bit after the addition. Do not output the carry bit or the sum in the function. Test your void function with these inputs 7,8 and 32, 255.
2. Defining bAnd, bin1, and bin2 as binary numbers as above, write the void function
void binaryAnd(int bAnd[],
const int bin1[], const int bin2[])
to compute bAnd as the AND of the two binary numbers bin1 and bin2. Do not output the binary number in the function. Test your function with 7,8 and 32, 255.
3. Write the void function for Problem 2 above renamed as binaryOr() using the OR operation.
Explanation / Answer
#define _CRT_SECURE_NO_DEPRECATE
#include<stdio.h>
void binaryAdd(int sum[], int *cBit, const int bin1[], const int bin2[]);
void binaryAnd(int bAnd[], const int bin1[], const int bin2[]);
void binaryOr(int bOr[], const int bin1[], const int bin2[]);
//ChecgEA written function to convert to binary and display in binary
void convertToBinary(int[], int num);
void display(int arr[]);
int main()
{
int bin1[8] = { 0 }, sum[8] = { 0 }, bin2[8] = { 0 };
int carry;
convertToBinary(bin1, 255); //change the second paramer passed to test for different number
convertToBinary(bin2, 32); //change the second paramer passed to test for different number
printf("Display bin1: ");
display(bin2);
printf("Display bin2: ");
display(bin1);
binaryAdd(sum, &carry, bin1, bin2);
printf("Dispplay Sum after addition of bin1 and bin2: ");
display(sum);
printf("carry = %d ", carry);
binaryAnd(sum, bin1, bin2);
printf("Dispplay Sum after and of bin1 and bin2: ");
display(sum);
binaryOr(sum, bin1, bin2);
printf("Dispplay Sum after OR of bin1 and bin2: ");
display(sum);
}
//checg EA convert to binary
void convertToBinary(int arr[], int num)
{
int rem;
int i = 7;
do
{
rem = num % 2;
arr[i--] = rem;
num /= 2 ;
} while (num > 0);
}
void display(int arr[])
{
int i;
for (i = 0; i <8; i++)
printf("%d ", arr[i]);
printf(" ");
}
void binaryAdd(int sum[], int *cBit, const int bin1[], const int bin2[])
{
int i,carry =0;
for (i = 7; i >=0; i--)
{
sum[i] = bin1[i] + bin2[i]+carry;
if (sum[i] == 2)
{
sum[i] %= 2;
carry = 1;
}
else
carry = 0;
}
*cBit = carry;
}
void binaryAnd(int bAnd[], const int bin1[], const int bin2[])
{
int i;
int bit1, bit2;
for (i = 0; i<8; i++)
{
bit1 = bin1[i];
bit2 = bin2[i];
bAnd[i] = bit1 & bit2;
}
}
void binaryOr(int bOr[], const int bin1[], const int bin2[])
{
int i;
int bit1, bit2;
for (i = 0; i<8; i++)
{
bit1 = bin1[i];
bit2 = bin2[i];
bOr[i] = bit1 | bit2;
}
}
------------------------------------------------------------------------
//output for 32 and 7
Display bin1: 0 0 1 0 0 0 0 0
Display bin2: 0 0 0 0 0 1 1 1
Dispplay Sum after addition of bin1 and bin2: 0 0 1 0 0 1 1 1
carry = 0
Dispplay Sum after and of bin1 and bin2: 0 0 0 0 0 0 0 0
Dispplay Sum after OR of bin1 and bin2: 0 0 1 0 0 1 1 1
//output with bin1 = 32 and bin2 =255
Display bin1: 0 0 1 0 0 0 0 0
Display bin2: 1 1 1 1 1 1 1 1
Dispplay Sum after addition of bin1 and bin2: 0 0 0 1 1 1 1 1
carry = 1
Dispplay Sum after and of bin1 and bin2: 0 0 1 0 0 0 0 0
Dispplay Sum after OR of bin1 and bin2: 1 1 1 1 1 1 1 1
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.