473,224 Members | 1,625 Online

# ~ bit-wise unary operator

Hello,
why ~ bit-wise unary operator returns -(x+1) and not bit inversion of
the given integer?

example:
a = 7978
a = ~a
python returns -7979

but I need to get back 57557 as in C language.

which is also in binary
0001111100101010
and inverted
1110000011010101

Is here any other operator or do I have to write it on my own?

Thank you,
Sep 26 '07 #1
6 5963
Hello,
why ~ bit-wise unary operator returns -(x+1) and not bit inversion of
the given integer?

example:
a = 7978
a = ~a
python returns -7979

but I need to get back 57557 as in C language.

which is also in binary
0001111100101010
and inverted
1110000011010101

Is here any other operator or do I have to write it on my own?
The size of int is an implementation detail in C, so you may not get
57557 (in fact, you're only likely to get that answer if a is an
unsigned short on any modern architecture. But if that's what you
want, try
def invert(x):
return ~x & 0xffff

--
Paul Hankin

Sep 26 '07 #2
Hello, why ~ bit-wise unary operator returns -(x+1) and not bit
inversion of the given integer?
On 2s-complement architectures, -(x+1) *is* bit inversion of the given
integer.
example:
a = 7978
a = ~a
python returns -7979

but I need to get back 57557 as in C language.
Python does exactly what C does in this case.

\$ cat a.c
#include <stdio.h>
int main(void)
{
int a = 7978;
a = ~a;
printf("%d\n", a);
return 0;
}
\$ gcc a.c
\$ ./a.out
-7979

If you want 16-bit unsigned arithmetic, use 2**16 + ~a, which yields
57557.
Sep 26 '07 #3
Hello,
why ~ bit-wise unary operator returns -(x+1) and not bit inversion of
the given integer?

example:
a = 7978
a = ~a
python returns -7979

but I need to get back 57557 as in C language.
It's not what C language returns, but if you want 57557, you
can get it like this:
>>(~7978) & 0xffff
57557

--
Grant Edwards grante Yow! Do you think the
at "Monkees" should get gas on
visi.com odd or even days?
Sep 26 '07 #4
cau,
maybe int is represented internally as a signed integer

you can use numpy types:
>>import numpy
~ numpy.uint16(7978)
57557

-m.
On Thu, 27 Sep 2007 00:14:49 +0200, Ladislav Andel wrote:
Hello,
why ~ bit-wise unary operator returns -(x+1) and not bit inversion of
the given integer?

example:
a = 7978
a = ~a
python returns -7979

but I need to get back 57557 as in C language.

which is also in binary
0001111100101010
and inverted
1110000011010101

Is here any other operator or do I have to write it on my own?

Thank you,
Sep 27 '07 #5
In message <87************@mulj.homelinux.net>, Hrvoje Niksic wrote:
If you want 16-bit unsigned arithmetic, use 2**16 + ~a, which yields
57557.
Or why not use "0xffff ^ a", which returns the same thing.
Sep 27 '07 #6
Wow, so many answers :). Thank you, guys :).

Michal Bozon wrote:
cau,
maybe int is represented internally as a signed integer

you can use numpy types:

>>>import numpy
~ numpy.uint16(7978)
57557

-m.
On Thu, 27 Sep 2007 00:14:49 +0200, Ladislav Andel wrote:

>Hello,
why ~ bit-wise unary operator returns -(x+1) and not bit inversion of
the given integer?

example:
a = 7978
a = ~a
python returns -7979

but I need to get back 57557 as in C language.

which is also in binary
0001111100101010
and inverted
1110000011010101

Is here any other operator or do I have to write it on my own?

Thank you,