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 6 5968
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
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.
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?
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
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.
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 This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics
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...
|
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 ?
...
|
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...
|
by: DolphinDB |
last post by:
Tired of spending countless mintues downsampling your data? Look no further!
In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
|
by: isladogs |
last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM).
In this month's session, we are pleased to welcome back...
|
by: jfyes |
last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
|
by: ArrayDB |
last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
|
by: PapaRatzi |
last post by:
Hello,
I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
|
by: CloudSolutions |
last post by:
Introduction:
For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
|
by: Defcon1945 |
last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
|
by: Shællîpôpï 09 |
last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
|
by: isladogs |
last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM).
In this session, we are pleased to welcome former...
| |