bitwiwse complement operator (~)

now in this blog we will discuss bitwise (~ ) operator :




so lets say for eg you are given a no in decimal format (means with base 2)

no is =5 whose binary format is in 32 bit compiler is given by




                                                 (where each digit represent a single bit)

so what (~) does is that it make the  0 to 1 and 1 to 0
so ~5 is given by

so how we know whats the decimal no of  ~5


so its -6 but how does it comes ??

if you convert the and do the calculation it will give some huge no...
(i was to lazy to convert it)
but it will be wrong .
why?
the left most bit represent the most significant bit if it is 0 the no is +ve
and it it is 1 the no is -ve

so way to calculate is that...
so the how -ve no are stored is ??
 eg take the no 5 
**-ve no are store using 2's complement of the no
so how to take the 2's complement 
1.) take the 1's complement 
2.) add 1 to 1's complement

so in order to get the ~5  we can take its 2's complement which 
will lead us to +ve of that given no.
so this is how we get -6 in the output
the formula for ~n= -(n+1)

(hope you liked it ,if you have any further question comment down below i would do my best to answer them ,thank you)

No comments:

Post a Comment

Contact Form

Name

Email *

Message *