Tuesday 12 February 2013

Finding 1’s Complement of a Binary Number is C

The one;s complement operator (~) is a unary operator that causes the bits of its operand to be inverted ( i. e. reversed) so that 1s becomes 0s and 0s becomes 1s. This operator always precedes its operand, The operand must be and integer-type. The one’s complement operator is sometimes referred to as the complementation operator. It is a member of the same precedence group as the other unary operations. Thus, its associativity is right to left. Now the following example illustrates the implementation of 1’s complement operator is a program,
Source Code:
#include <stdio.h>
int main(){
    unsigned i = 0x7fff;
    printf("hexadecimal value: i = %x   ~i = %x\n", i, ~i);
    printf("decimal value: i = %u    ~i = %u\n", i, ~i);
    return 0;
}

Output

hexadecimal value: i = 7fff   ~i = ffff8000
decimal value: i = 32767    ~i = 4294934528

No comments:

Post a Comment

Comment