Sign in to follow this  
sevak

compl

Recommended Posts

Fruny    1658
In C (and C++), compl is an equivalent keyword for ~, which is the bitwise complement operator. That is, all the bits in num get flipped.

000000000000000000000000000000012 becomes 111111111111111111111111111111102 which is the 32-bit 2-complement signed representation of -2.

Share this post


Link to post
Share on other sites
rypyr    252
Not without you explaining what compl() is :P

I assume it's the one's complement...flip all the bits (so all 0s become 1s, all 1s become 0s). This leaves you with a number that has all bits set to 1 except the least significant bit.

00000000000000000000000000000001 => 1
(take one's complement)
11111111111111111111111111111110 => -2

(since this is a signed integer, the most significant bit being one indicates that the value is negative).

Regards,
Jeff

Share this post


Link to post
Share on other sites
First you need to know what the compl function does, which is the
same as ~. That is to say, it returns the number with all bits reversed. So if you were working with unsigned 8 bit numbers

00000001 = 1
11111110= ~1 = 254

The difference is that int is by default signed. In this case you need to refer to the two's complement in reference to the bevahior of negative numbers in binary. The short and easy version is basically that:

11111110
+ 1
---------
11111111
+ 1
---------
100000000

And because 100000000 is 9 bits, instead of 8, the number is truncated to just plain zero. 11111110 as a signed char is -2 because adding 2 to it makes zero.

For more reading on the two's complement, look here.

[edit] Figures I was beaten to it. Many times. [sad]

Share this post


Link to post
Share on other sites
Fruny    1658
Quote:
Original post by sevak
is it possible to make ~ work with only one byte so I don't get this problem?


Maybe you are looking for the logical-not operator ?

int num = 1;
cout << !num << endl;


Which prints 0, or do you really mean

int num = 1;
cout << static_cast<int>(static_cast<unsigned char>(~num)) << endl;


Which yields 254 (i.e. 000000000000000000000000111111102)

Share this post


Link to post
Share on other sites
rypyr    252
Quote:
Original post by Fruny
In C (and C++), compl is an equivalent keyword for ~, which is the bitwise complement operator.


Interesting, never knew that...

Share this post


Link to post
Share on other sites
rypyr    252
Quote:
Original post by sevak
is it possible to make ~ work with only one byte so I don't get this problem?


What are you trying to do exactly?

Share this post


Link to post
Share on other sites
sevak    100
I guess I didn't realy understand what ~ meant. Thanks now I get it. I guess what I was trying to do was use the ~(compl) like a !(not) opertator

Share this post


Link to post
Share on other sites
Zahlman    1682
Quote:
Original post by Fruny
Which prints 0, or do you really mean

int num = 1;
cout << static_cast<int>(static_cast<unsigned char>(~num)) << endl;


Which yields 254 (i.e. 000000000000000000000000111111102)


Ok, but why not just

int num = 1;
cout << num ^ 255;


o_O

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this