#include <stdio.
h>
#include <stdint.h>
#include <math.h>
// Function to convert decimal to binary fraction
void decimalToBinaryFraction(float num, int fractionBits, int binaryFraction[]) {
// Take the absolute value for simplicity
num = (num < 0) ? -num : num;
// Extract the integer part, exponent, and fractional part
int integerPart = (int)num;
float fractionalPart = num - integerPart;
// Calculate the binary fraction using the algorithm you described
for (int i = 0; i < fractionBits; ++i) {
fractionalPart *= 2;
binaryFraction[i] = (int)fractionalPart;
fractionalPart -= (int)fractionalPart;
}
}
// Function to convert binary fraction to decimal
float binaryFractionToDecimal(int binaryFraction[], int fractionBits) {
float result = 0.0;
float factor = 0.5; // Initial factor for the first bit
// Sum up the binary fraction using the algorithm you described
for (int i = 0; i < fractionBits; ++i) {
result += binaryFraction[i] * factor;
factor /= 2;
}
return result;
}
int main() {
float decimalNum;
// Get user input
printf("Enter a decimal number: ");
scanf("%f", &decimalNum);
// Number of bits for the fraction in IEEE 754 single-precision
int fractionBits = 23;
// Array to store the binary representation of the fraction
int binaryFraction[fractionBits];
// Convert decimal to binary fraction
decimalToBinaryFraction(decimalNum, fractionBits, binaryFraction);
// Display binary components and decimal value
printf("Binary Fraction: ");
for (int i = 0; i < fractionBits; ++i) {
printf("%d", binaryFraction[i]);
}
printf("\n");
// Display decimal value
float decimalValue = binaryFractionToDecimal(binaryFraction, fractionBits);
printf("Decimal Value: %f\n", decimalValue);
return 0;
}
#include <stdio.h>
#include <stdint.h>
// Function to convert decimal to 32-bit IEEE 754 binary representation
void decimalToBinary(float num, char binaryRepresentation[]) {
// Extract the sign, exponent, and fraction
uint32_t* binaryNum = (uint32_t*)#
// Display binary representation
for (int i = 31; i >= 0; --i) {
binaryRepresentation[i] = ((*binaryNum) & 1) + '0';
(*binaryNum) >>= 1;
}
binaryRepresentation[32] = '\0'; // Null-terminate the string
}
#include <stdio.h>
#include <stdint.h>
#include <math.h>
// Function to convert 32-bit IEEE 754 binary representation to decimal
float binaryToDecimal(char binaryRepresentation[]) {
uint32_t binaryNum = 0;
// Convert binary representation to uint32_t
for (int i = 0; i < 32; ++i) {
binaryNum = (binaryNum << 1) | (binaryRepresentation[i] - '0');
}
// Interpret uint32_t as a float
float result;
memcpy(&result, &binaryNum, sizeof(float));
return result;
}
int main() {
// Array to store the binary representation
char binaryRepresentation[33];
// Get user input
printf("Enter the binary representation (32 bits): ");
scanf("%32s", binaryRepresentation);
// Convert binary to decimal
float decimalValue = binaryToDecimal(binaryRepresentation);
// Display decimal value
printf("Decimal Value: %f\n", decimalValue);
return 0;
}
int main() {
float decimalNum;
// Get user input
printf("Enter a decimal number: ");
scanf("%f", &decimalNum);
// Array to store the binary representation
char binaryRepresentation[33];
// Convert decimal to binary representation
decimalToBinary(decimalNum, binaryRepresentation);
// Display binary representation
printf("Binary Representation (32 bits): %s\n", binaryRepresentation);
return 0;
}
#include <stdio.h>
#include <stdint.h>
// Function to convert binary representation to decimal integer
int binaryToInt(char binaryRepresentation[], int start, int end) {
int binaryNum = 0;
// Convert binary representation to integer
for (int i = start; i <= end; ++i) {
binaryNum = binaryNum * 2 + (binaryRepresentation[i] - '0');
}
return binaryNum;
}
// Function to convert 32-bit IEEE 754 binary representation to decimal
float binaryToDecimal(char binaryRepresentation[]) {
// Extract sign, exponent, and fraction parts
int sign = binaryRepresentation[0] - '0';
int exponent = binaryToInt(binaryRepresentation, 1, 8) - 127;
int fraction = binaryToInt(binaryRepresentation, 9, 31);
// Calculate the decimal value without using bitwise shifts
float result = 1.0;
// Calculate the fractional part
for (int i = 0; i < 23; ++i) {
result += ((fraction >> (22 - i)) & 1) * (1.0 / (1 << (i + 1)));
}
// Apply the sign and exponent
result *= (sign == 1) ? -1 : 1;
result *= pow(2, exponent);
return result;
}
int main() {
// Array to store the binary representation
char binaryRepresentation[33];
// Get user input
printf("Enter the binary representation (32 bits): ");
scanf("%32s", binaryRepresentation);
// Convert binary to decimal
float decimalValue = binaryToDecimal(binaryRepresentation);
// Display decimal value
printf("Decimal Value: %f\n", decimalValue);
return 0;
}
#include <stdio.h>
#include <string.h>
// Function to add two binary strings
void addBinary(char* a, char* b, char* result) {
int len_a = strlen(a);
int len_b = strlen(b);
int carry = 0;
int max_len = (len_a > len_b) ? len_a : len_b;
result[max_len + 1] = '\0';
for (int i = 0; i < max_len; ++i) {
int bit_a = (i < len_a) ? (a[len_a - 1 - i] - '0') : 0;
int bit_b = (i < len_b) ? (b[len_b - 1 - i] - '0') : 0;
int sum = bit_a + bit_b + carry;
result[max_len - i] = (sum % 2) + '0';
carry = sum / 2;
}
result[0] = carry + '0';
}
int main() {
char binaryNum1[100], binaryNum2[100], sum[101];
// Input two binary numbers
printf("Enter the first binary number: ");
scanf("%s", binaryNum1);
printf("Enter the second binary number: ");
scanf("%s", binaryNum2);
// Perform addition
addBinary(binaryNum1, binaryNum2, sum);
// Display the result
printf("Sum: %s\n", sum);
return 0;
}
#include <stdio.h>
#include <string.h>
// Function to multiply two binary strings
void multiplyBinary(char* a, char* b, char* result) {
int len_a = strlen(a);
int len_b = strlen(b);
int result_len = len_a + len_b;
result[result_len] = '\0';
for (int i = 0; i < result_len; ++i) {
result[i] = '0';
}
for (int i = 0; i < len_a; ++i) {
for (int j = 0; j < len_b; ++j) {
int bit_a = a[len_a - 1 - i] - '0';
int bit_b = b[len_b - 1 - j] - '0';
int product = bit_a * bit_b + (result[len_a + len_b - i - j - 1] - '0');
result[len_a + len_b - i - j - 1] = (product % 2) + '0';
result[len_a + len_b - i - j - 2] = ((product / 2) + (result[len_a + len_b - i - j - 2] - '0')) + '0';
}
}
}
int main() {
char binaryNum1[100], binaryNum2[100], product[200];
// Input two binary numbers
printf("Enter the first binary number: ");
scanf("%s", binaryNum1);
printf("Enter the second binary number: ");
scanf("%s", binaryNum2);
// Perform multiplication
multiplyBinary(binaryNum1, binaryNum2, product);
// Display the result
printf("Product: %s\n", product);
return 0;
}
#include <stdio.h>
#include <string.h>
// Function to subtract two binary strings
void subtractBinary(char* a, char* b, char* result) {
int len_a = strlen(a);
int len_b = strlen(b);
result[len_a] = '\0';
int borrow = 0;
for (int i = 0; i < len_a; ++i) {
int bit_a = a[len_a - 1 - i] - '0';
int bit_b = (i < len_b) ? (b[len_b - 1 - i] - '0') : 0;
int difference = bit_a - bit_b - borrow;
if (difference < 0) {
difference += 2;
borrow = 1;
} else {
borrow = 0;
}
result[len_a - 1 - i] = difference + '0';
}
}
// Function to divide two binary strings
void divideBinary(char* dividend, char* divisor, char* quotient) {
int len_dividend = strlen(dividend);
int len_divisor = strlen(divisor);
quotient[len_dividend] = '\0';
char tempDividend[100];
tempDividend[len_divisor] = '\0';
for (int i = 0; i < len_dividend; ++i) {
// Append the next bit from the dividend to the temporary dividend
strncat(tempDividend, ÷nd[i], 1);
// Check if the temporary dividend is greater than or equal to the divisor
if (strcmp(tempDividend, divisor) >= 0) {
// Subtract the divisor from the temporary dividend
subtractBinary(tempDividend, divisor, tempDividend);
// Set the corresponding quotient bit to 1
quotient[i] = '1';
} else {
// Set the corresponding quotient bit to 0
quotient[i] = '0';
}
}
}
int main() {
char binaryNum1[100], binaryNum2[100], quotient[100];
// Input two binary numbers
printf("Enter the dividend: ");
scanf("%s", binaryNum1);
printf("Enter the divisor: ");
scanf("%s", binaryNum2);
// Perform division
divideBinary(binaryNum1, binaryNum2, quotient);
// Display the result
printf("Quotient: %s\n", quotient);
return 0;
}
#include <stdio.h>
#include <string.h>
// Function to subtract two binary strings
void subtractBinary(char* a, char* b, char* result) {
int len_a = strlen(a);
int len_b = strlen(b);
result[len_a] = '\0';
int borrow = 0;
for (int i = 0; i < len_a; ++i) {
int bit_a = a[len_a - 1 - i] - '0';
int bit_b = (i < len_b) ? (b[len_b - 1 - i] - '0') : 0;
int difference = bit_a - bit_b - borrow;
if (difference < 0) {
difference += 2;
borrow = 1;
} else {
borrow = 0;
}
result[len_a - 1 - i] = difference + '0';
}
}
int main() {
char binaryNum1[100], binaryNum2[100], difference[100];
// Input two binary numbers
printf("Enter the first binary number: ");
scanf("%s", binaryNum1);
printf("Enter the second binary number: ");
scanf("%s", binaryNum2);
// Perform subtraction
subtractBinary(binaryNum1, binaryNum2, difference);
// Display the result
printf("Difference: %s\n", difference);
return 0;
}
#include <stdio.h>
#include <string.h>
// Function to add two binary strings
void addBinary(char* a, char* b, char* result) {
int len_a = strlen(a);
int len_b = strlen(b);
int carry = 0;
int max_len = (len_a > len_b) ? len_a : len_b;
result[max_len + 1] = '\0';
for (int i = 0; i < max_len; ++i) {
int bit_a = (i < len_a) ? (a[len_a - 1 - i] - '0') : 0;
int bit_b = (i < len_b) ? (b[len_b - 1 - i] - '0') : 0;
int sum = bit_a + bit_b + carry;
result[max_len - i] = (sum % 2) + '0';
carry = sum / 2;
}
result[0] = carry + '0';
}
// Function to add two floating-point decimal strings
void addFloatingPoint(char* a, char* b, char* result) {
char integerPartA[50], fractionalPartA[50], integerPartB[50], fractionalPartB[50];
sscanf(a, "%[^.].%s", integerPartA, fractionalPartA);
sscanf(b, "%[^.].%s", integerPartB, fractionalPartB);
char sumInteger[50], sumFractional[50];
// Add the integer parts
addBinary(integerPartA, integerPartB, sumInteger);
// Add the fractional parts
addBinary(fractionalPartA, fractionalPartB, sumFractional);
// Handle any carry from the fractional addition
if (sumFractional[0] == '2') {
sumFractional[0] = '0';
addBinary(sumInteger, "1", sumInteger);
}
// Concatenate the result
sprintf(result, "%s.%s", sumInteger, sumFractional);
}
int main() {
char num1[100], num2[100], sum[100];
// Input two floating-point decimal numbers
printf("Enter the first number: ");
scanf("%s", num1);
printf("Enter the second number: ");
scanf("%s", num2);
// Perform addition
addFloatingPoint(num1, num2, sum);
// Display the result
printf("Sum: %s\n", sum);
return 0;
}
#include <stdio.h>
#include <string.h>
// Function to subtract two binary strings
void subtractBinary(char* a, char* b, char* result) {
int len_a = strlen(a);
int len_b = strlen(b);
result[len_a] = '\0';
int borrow = 0;
for (int i = 0; i < len_a; ++i) {
int bit_a = a[len_a - 1 - i] - '0';
int bit_b = (i < len_b) ? (b[len_b - 1 - i] - '0') : 0;
int difference = bit_a - bit_b - borrow;
if (difference < 0) {
difference += 2;
borrow = 1;
} else {
borrow = 0;
}
result[len_a - 1 - i] = difference + '0';
}
}
// Function to subtract two floating-point decimal strings
void subtractFloatingPoint(char* a, char* b, char* result) {
char integerPartA[50], fractionalPartA[50], integerPartB[50], fractionalPartB[50];
sscanf(a, "%[^.].%s", integerPartA, fractionalPartA);
sscanf(b, "%[^.].%s", integerPartB, fractionalPartB);
char differenceInteger[50], differenceFractional[50];
// Subtract the integer parts
subtractBinary(integerPartA, integerPartB, differenceInteger);
// Subtract the fractional parts
subtractBinary(fractionalPartA, fractionalPartB, differenceFractional);
// Handle any borrow from the fractional subtraction
if (differenceFractional[0] == '-') {
differenceFractional[0] = '0';
subtractBinary(differenceInteger, "1", differenceInteger);
}
// Concatenate the result
sprintf(result, "%s.%s", differenceInteger, differenceFractional);
}
int main() {
char num1[100], num2[100], difference[100];
// Input two floating-point decimal numbers
printf("Enter the first number: ");
scanf("%s", num1);
printf("Enter the second number: ");
scanf("%s", num2);
// Perform subtraction
subtractFloatingPoint(num1, num2, difference);
// Display the result
printf("Difference: %s\n", difference);
return 0;
}
#include <stdio.h>
#include <string.h>
// Function to multiply two binary strings
void multiplyBinary(char* a, char* b, char* result) {
int len_a = strlen(a);
int len_b = strlen(b);
int result_len = len_a + len_b;
result[result_len] = '\0';
for (int i = 0; i < result_len; ++i) {
result[i] = '0';
}
for (int i = 0; i < len_a; ++i) {
for (int j = 0; j < len_b; ++j) {
int bit_a = a[len_a - 1 - i] - '0';
int bit_b = b[len_b - 1 - j] - '0';
int product = bit_a * bit_b + (result[len_a + len_b - i - j - 1] - '0');
result[len_a + len_b - i - j - 1] = (product % 2) + '0';
result[len_a + len_b - i - j - 2] = ((product / 2) + (result[len_a + len_b - i - j - 2] - '0')) + '0';
}
}
}
// Function to multiply two floating-point decimal strings
void multiplyFloatingPoint(char* a, char* b, char* result) {
char integerPartA[50], fractionalPartA[50], integerPartB[50], fractionalPartB[50];
sscanf(a, "%[^.].%s", integerPartA, fractionalPartA);
sscanf(b, "%[^.].%s", integerPartB, fractionalPartB);
char productInteger[50], productFractional[100];
// Multiply the integer parts
multiplyBinary(integerPartA, integerPartB, productInteger);
// Multiply the fractional parts
multiplyBinary(fractionalPartA, fractionalPartB, productFractional);
// Handle the fractional part
int len_productFractional = strlen(productFractional);
for (int i = len_productFractional - 1; i >= 0; --i) {
if (productFractional[i] == '1') {
break;
} else {
productFractional[i] = '\0';
}
}
// Concatenate the result
sprintf(result, "%s.%s", productInteger, productFractional);
}
int main() {
char num1[100], num2[100], product[200];
// Input two floating-point decimal numbers
printf("Enter the first number: ");
scanf("%s", num1);
printf("Enter the second number: ");
scanf("%s", num2);
// Perform multiplication
multiplyFloatingPoint(num1, num2, product);
// Display the result
printf("Product: %s\n", product);
return 0;
}
#include <stdio.h>
#include <string.h>
// Function to subtract two binary strings
void subtractBinary(char* a, char* b, char* result) {
int len_a = strlen(a);
int len_b = strlen(b);
result[len_a] = '\0';
int borrow = 0;
for (int i = 0; i < len_a; ++i) {
int bit_a = a[len_a - 1 - i] - '0';
int bit_b = (i < len_b) ? (b[len_b - 1 - i] - '0') : 0;
int difference = bit_a - bit_b - borrow;
if (difference < 0) {
difference += 2;
borrow = 1;
} else {
borrow = 0;
}
result[len_a - 1 - i] = difference + '0';
}
}
// Function to divide two binary strings
void divideBinary(char* dividend, char* divisor, char* quotient) {
int len_dividend = strlen(dividend);
int len_divisor = strlen(divisor);
quotient[len_dividend] = '\0';
char tempDividend[100];
tempDividend[len_divisor] = '\0';
for (int i = 0; i < len_dividend; ++i) {
// Append the next bit from the dividend to the temporary dividend
strncat(tempDividend, ÷nd[i], 1);
if (strcmp(tempDividend, divisor) >= 0) {
// Subtract the divisor from the temporary dividend
subtractBinary(tempDividend, divisor, tempDividend);
// Set the corresponding quotient bit to 1
quotient[i] = '1';
} else {
// Set the corresponding quotient bit to 0
quotient[i] = '0';
}
}
}
// Function to divide two floating-point decimal strings
void divideFloatingPoint(char* dividend, char* divisor, char* result) {
char integerPartDividend[50], fractionalPartDividend[50], integerPartDivisor[50],
fractionalPartDivisor[50];
sscanf(dividend, "%[^.].%s", integerPartDividend, fractionalPartDividend);
sscanf(divisor, "%[^.].%s", integerPartDivisor, fractionalPartDivisor);
char quotientInteger[50], quotientFractional[100];
// Divide the integer parts
divideBinary(integerPartDividend, integerPartDivisor, quotientInteger);
// Divide the fractional parts
divideBinary(fractionalPartDividend, fractionalPartDivisor, quotientFractional);
// Concatenate the result
sprintf(result, "%s.%s", quotientInteger, quotientFractional);
}
int main() {
char num1[100], num2[100], quotient[100];
// Input two floating-point decimal numbers
printf("Enter the dividend: ");
scanf("%s", num1);
printf("Enter the divisor: ");
scanf("%s", num2);
// Perform division
divideFloatingPoint(num1, num2, quotient);
// Display the result
printf("Quotient: %s\n", quotient);