471,357 Members | 1,087 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 471,357 software developers and data experts.

~ 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,
Lada
Sep 26 '07 #1
6 5887
On Sep 26, 11:14 pm, Ladislav Andel <lad...@iptel.orgwrote:
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
Ladislav Andel <la****@iptel.orgwrites:
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
On 2007-09-26, Ladislav Andel <la****@iptel.orgwrote:
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,
Lada
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 :).

Lada

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,
Lada

Sep 27 '07 #7

This discussion thread is closed

Replies have been disabled for this discussion.

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.