This is a naive question:
"%u" % -3
I expect it to print 3. But it still print -3.
Also, if I have an int, I can convert it to unsigned int in C:
int i = -3;
int ui = (unsigned int)i;
Is there a way to do this in Python? 14 52163
On Mar 10, 11:32 am, "Jack" <nos...@invalid.comwrote:
This is a naive question:
"%u" % -3
I expect it to print 3. But it still print -3.
Also, if I have an int, I can convert it to unsigned int in C:
int i = -3;
int ui = (unsigned int)i;
Is there a way to do this in Python?
def unsigned(n):
return n & 0xFFFFFFFF
"Jack" <no****@invalid.comwrote:
This is a naive question:
"%u" % -3
I expect it to print 3. But it still print -3.
Internally it uses the C runtime to format the number, but if the number
you ask it to print unsigned is negative it uses %d instead of %u. I have
no idea if it is actually possibly to get a different output for %d versus
%u.
>
Also, if I have an int, I can convert it to unsigned int in C:
int i = -3;
int ui = (unsigned int)i;
Is there a way to do this in Python?
Depeneding on how exactly you want it converted:
i = -3
ui = abs(i)
print ui
ui = (i & 0xffff) # for 16 bit integers
print ui
ui = (i & 0xffffffff) # for 32 bit integers
print ui
ui = (i & 0xffffffffffffffff) # for 64 bit integers
print ui
ui = (i & 0xffffffffffffffffffffffffffffffff) # for 128 bit integers
print ui
which gives the following output:
3
65533
4294967293
18446744073709551613
340282366920938463463374607431768211453
There isn't a unique way to convert a Python integer to an unsigned value
which is why the %u format string cannot do anything other than print the
value. Personally I'd have expected the Python one to either print the
absolute value or throw an exception, but I guess making it an alias for %d
kind of makes sense as well.
Dan Bishop wrote:
On Mar 10, 11:32 am, "Jack" <nos...@invalid.comwrote:
>This is a naive question:
"%u" % -3
I expect it to print 3. But it still print -3.
Also, if I have an int, I can convert it to unsigned int in C: int i = -3; int ui = (unsigned int)i;
Is there a way to do this in Python?
def unsigned(n):
return n & 0xFFFFFFFF
or abs(-1) ?
On Mar 10, 11:50 am, Duncan Booth <duncan.bo...@invalid.invalid>
wrote:
"Jack" <nos...@invalid.comwrote:
This is a naive question:
"%u" % -3
I expect it to print 3. But it still print -3.
Internally it uses the C runtime to format the number, but if the number
you ask it to print unsigned is negative it uses %d instead of %u. I have
no idea if it is actually possibly to get a different output for %d versus
%u.
%u used to be different from %d, but it changed because of the int/
long unification in Python 2.4.
"Dan Bishop" <da*****@yahoo.comwrote:
On Mar 10, 11:50 am, Duncan Booth <duncan.bo...@invalid.invalid>
wrote:
>"Jack" <nos...@invalid.comwrote:
This is a naive question:
"%u" % -3
I expect it to print 3. But it still print -3.
Internally it uses the C runtime to format the number, but if the number you ask it to print unsigned is negative it uses %d instead of %u. I have no idea if it is actually possibly to get a different output for %d versus %u.
%u used to be different from %d, but it changed because of the int/
long unification in Python 2.4.
Yes, I guessed that was it.
The implementation is identical when the value is negative but still
different when the integer is non-negative which is why I questioned
whether it was actually possible to get different output. If not perhaps
both the implementation and the documentation should be simplified.
Thanks for all the replies. Because I want to convert an int,
Dan's function actually does it well.
"Jack" <no****@invalid.comwrote in message
news:I9******************************@comcast.com. ..
This is a naive question:
"%u" % -3
I expect it to print 3. But it still print -3.
Also, if I have an int, I can convert it to unsigned int in C:
int i = -3;
int ui = (unsigned int)i;
Is there a way to do this in Python?
"Jack" <no****@invalid.comwrites:
Also, if I have an int, I can convert it to unsigned int in C:
int i = -3;
int ui = (unsigned int)i;
I just tried it:
main() {
int i = -3;
unsigned int ui = i;
printf("%d\n", ui);
}
prints -3. What do you want the conversion to do? If you want
the absolute value, use abs().
En Sat, 10 Mar 2007 16:26:08 -0300, Paul Rubin
<"http://phr.cx"@NOSPAM.invalidescribió:
"Jack" <no****@invalid.comwrites:
>Also, if I have an int, I can convert it to unsigned int in C: int i = -3; int ui = (unsigned int)i;
I just tried it:
main() {
int i = -3;
unsigned int ui = i;
printf("%d\n", ui);
}
prints -3. What do you want the conversion to do? If you want
the absolute value, use abs().
Try again with "%u". Passing i or ui makes no difference, both push the
same value on the stack. C relies on the format string to interpret the
arguments.
--
Gabriel Genellina
"Gabriel Genellina" <ga*******@yahoo.com.arwrites:
Try again with "%u". Passing i or ui makes no difference, both push
the same value on the stack. C relies on the format string to
interpret the arguments.
If you use %u you get a very large positive value, not +3.
En Sat, 10 Mar 2007 20:26:13 -0300, Paul Rubin
<"http://phr.cx"@NOSPAM.invalidescribió:
"Gabriel Genellina" <ga*******@yahoo.com.arwrites:
>Try again with "%u". Passing i or ui makes no difference, both push the same value on the stack. C relies on the format string to interpret the arguments.
If you use %u you get a very large positive value, not +3.
Exactly, and that's the right value. (unsigned int)(-3) isn't +3.
--
Gabriel Genellina
"Gabriel Genellina" <ga*******@yahoo.com.arwrites:
If you use %u you get a very large positive value, not +3.
Exactly, and that's the right value. (unsigned int)(-3) isn't +3.
The OP specified that the expected result was 3.
Paul Rubin wrote:
The OP specified that the expected result was 3.
But that's not what he'd get with his C conversion ;)
Regards,
Björn
--
BOFH excuse #348:
We're on Token Ring, and it looks like the token got loose.
hg wrote:
Dan Bishop wrote:
>def unsigned(n): return n & 0xFFFFFFFF
or abs(-1) ?
Nah! Bitwise operators are cool. ;)
Though ANDing won't make the int unsigned.
Regards,
Björn
--
BOFH excuse #23:
improperly oriented keyboard
En Sat, 10 Mar 2007 21:04:00 -0300, Paul Rubin
<"http://phr.cx"@NOSPAM.invalidescribió:
"Gabriel Genellina" <ga*******@yahoo.com.arwrites:
If you use %u you get a very large positive value, not +3.
Exactly, and that's the right value. (unsigned int)(-3) isn't +3.
The OP specified that the expected result was 3.
Ouch! Yes, sorry, I overlooked it. And the C code just made things more
confusing.
--
Gabriel Genellina This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics
by: Andy |
last post by:
Hi,
Are 1 through 4 defined behaviors in C?
unsigned short i;
unsigned long li; /* 32-bit wide */
1. i = 65535 + 3;
2. i = 1 - 3;
3. li = (unsigned long)0xFFFFFFFF + 3;
4. li = 1...
|
by: Vijay Kumar R Zanvar |
last post by:
Hi clc,
Please elaborate the warning:
F:\Vijay\C> type unsigned2.c
#include <stdio.h>
#include <stdlib.h>
int
|
by: Peter Ammon |
last post by:
When I add an unsigned long long and an int, what type do each of the
values get promoted to before the addition is performed? What is the
type of the resulting expression? What occurs if the...
|
by: Mike Miller |
last post by:
What is the best way to convert a managed unsigned int64 to an
unsigned long? Basically I need to do the following:
System::UInt64 managedInt = 10;
unsigned long unmanagedInt;
unmanagedInt =...
|
by: luke |
last post by:
Hi everybody,
please, can someone explain me this behaviour.
I have the following piece of code:
long long ll;
unsigned int i = 2;
ll = -1 * i;
printf("%lld\n", ll);
|
by: Christian Christmann |
last post by:
Hi,
what does the ANSI C-99 standard says when an unsigned variable
is assigned a negative number?, i.e.
unsigned char a = -100;
Is this undefined behavior or should the negative number...
|
by: techie |
last post by:
I have defined a number of unsigned integer types as follows:
typedef unsigned char uint8;
typedef unsigned short uint16;
typedef unsigned int uint32;
typedfe long long uint64;
Is it...
|
by: Jim Langston |
last post by:
Is the following well defined?
size_t IntVal = 65537;
unsigned short Length;
if ( IntVal static_cast<unsigned short>( -1 ) )
{
std::cout << "Value too long to fit in a short" << std::endl;...
|
by: Paulo Matos |
last post by:
Hello,
Is it safe to assume a size_t is an unsigned long? (is it forced by
the standard?)
Thank you,
Paulo Matos
|
by: somenath |
last post by:
Hi All,
I am trying to undestand "Type Conversions" from K&R book.I am not
able to understand the
bellow mentioned text
"Conversion rules are more complicated when unsigned operands are...
|
by: Charles Arthur |
last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
|
by: BarryA |
last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
|
by: Hystou |
last post by:
There are some requirements for setting up RAID:
1. The motherboard and BIOS support RAID configuration.
2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
|
by: marktang |
last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
|
by: Hystou |
last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
|
by: Oralloy |
last post by:
Hello folks,
I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>".
The problem is that using the GNU compilers,...
|
by: jinu1996 |
last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
|
by: Hystou |
last post by:
Overview:
Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
|
by: agi2029 |
last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
| |