C program that takes as input an 8-bit number perform and print the logical NEG of the number
Posted by Samath
Last Updated: July 01, 2015

Write a C program that takes as input an 8-bit number. Perform and print the logical NEG of this number. 

This program looks at the binary representation of the values of the expression and does a bitwise negation operation on it. Any digit that is a 1 in the expression becomes a 0 in the result. Any digit that is a 0 in the expression becomes a 1 in the result.

Code:

#include <stdio.h>
#include <stdlib.h>

int main(int argc, char *argv[])
{
	int binary[8]; 
	

	for(int i=0; i<8; i++)
	{
		int bit = -1; 
		while(bit == -1)
		{
			printf("Enter a 8-bit (Enter O and 1 Only): ");
			scanf("%d", &bit);
			if(bit != 0 && bit != 1)
			bit = -1;
		}
		
		binary[i] = bit;
	}
	
	printf("The binary number you entered is : ");
	for(int i = 0; i<8; i++)
	{
		printf("%d", binary[i]);
	}

	for(int i=0; i<8; i++)
	{
		if(binary[i]==0)
		binary[i] = 1;
		else
		binary[i]=0;
	}
	
	printf("\nApplying the NEG operator to the binary number results: ");
	for(int i = 0; i<8; i++)
	{
		printf("%d", binary[i]);
	}
	
	return 0;
}