473,224 Members | 1,625 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,224 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 5963
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: Skybuck Flying | last post by:
Hi, I think I might have just invented the variable bit cpu :) It works simply like this: Each "data bit" has a "meta data bit". The meta data bit describes if the bit is the ending bit...
12
by: Jean-Marc Blaise | last post by:
Hi, Is it worth to use 64-bit DB2 instances on a 32-bit kernel, in terms of: - performance - configuration (go beyond the 256 Mb segment for private mem, 1.75 Gb for Bufferpools) - other ? ...
58
by: Larry David | last post by:
Ok, first of all, let's get the obvious stuff out of the way. I'm an idiot. So please indulge me for a moment. Consider it an act of "community service".... What does "64bit" mean to your friendly...
1
isladogs
by: isladogs | last post by:
The next online meeting of the Access Europe User Group will be on Wednesday 6 Dec 2023 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, Mike...
0
by: VivesProcSPL | last post by:
Obviously, one of the original purposes of SQL is to make data query processing easy. The language uses many English-like terms and syntax in an effort to make it easy to learn, particularly for...
3
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 3 Jan 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). For other local times, please check World Time Buddy In...
0
by: jianzs | last post by:
Introduction Cloud-native applications are conventionally identified as those designed and nurtured on cloud infrastructure. Such applications, rooted in cloud technologies, skillfully benefit from...
0
by: abbasky | last post by:
### Vandf component communication method one: data sharing ​ Vandf components can achieve data exchange through data sharing, state sharing, events, and other methods. Vandf's data exchange method...
2
by: jimatqsi | last post by:
The boss wants the word "CONFIDENTIAL" overlaying certain reports. He wants it large, slanted across the page, on every page, very light gray, outlined letters, not block letters. I thought Word Art...
0
by: fareedcanada | last post by:
Hello I am trying to split number on their count. suppose i have 121314151617 (12cnt) then number should be split like 12,13,14,15,16,17 and if 11314151617 (11cnt) then should be split like...
0
by: stefan129 | last post by:
Hey forum members, I'm exploring options for SSL certificates for multiple domains. Has anyone had experience with multi-domain SSL certificates? Any recommendations on reliable providers or specific...
1
by: davi5007 | last post by:
Hi, Basically, I am trying to automate a field named TraceabilityNo into a web page from an access form. I've got the serial held in the variable strSearchString. How can I get this into the...

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.