samprat samprat - 2 months ago 9
C Question

how to use inverse in C

[how to use ~ operator ]

I have a structure say

Alpha
. I know the value of element inside
Alpha
(say
a
) which can be
0
or
1
- I want the other element of same structure to take inverse value of Alpha.a. For example:

if Alpha.a = 1
then Alpha.b = 0


and vice versa

I have tried:

Alpha.b = ~ (Alpha.a)


But unfortunately it doesnt work - when
Alpha.a
is
1
,
Alpha.b
gets set to
254


Any ideas?

Thanks and regards,

SamPrat

Answer

Use XOR operator:

Alpha.b = Alpha.a ^ 1;
Comments