On Fri, 17 Jun 2011 23:14:09 -0700, jmfauth wrote:
'{:+#0{}b}'.format(255, 1 + 2 + 16)
> +0b
'{:+#0{}b}'.format(-255, 1 + 2 + 16)
> -0b
eval('{:+#0{}b}'.format(255, 1 + 2 + 16))
> 255
eval('{:+#0{}b}'.format(-255, 1 + 2 + 16))
> -255
Is th
>>> '{:+#0{}b}'.format(255, 1 + 2 + 16)
+0b
>>> '{:+#0{}b}'.format(-255, 1 + 2 + 16)
-0b
>>>
>>> eval('{:+#0{}b}'.format(255, 1 + 2 + 16))
255
>>> eval('{:+#0{}b}'.format(-255, 1 + 2 + 16))
-255
>>>
jmf
--
http://mail.python.org/mailman/listinfo/python-list
On Jun 15, 9:33 am, Olivier LEMAIRE
wrote:
> You're right, I use Python 2.6.6
This works great in 2.6.5 and later (and probably earlier). You just
have to number your placeholders. The first set of braces formats i
(your value), the second set specifies the field with (i.e., 8):
>>> for i in xra
You're right, I use Python 2.6.6
--
http://mail.python.org/mailman/listinfo/python-list
On 06/15/2011 07:33 AM, Daniel Rentz wrote:
Am 15.06.2011 14:29, schrieb Olivier LEMAIRE:
Hi there, I've been looking for 2 days for a way to convert integer
to binary number 0-padded, nothing... I need to get numbers converted
with a defined number of bits. For example on 8 bits 2 = 0010
Thank you to all of you !!
so finally, I can simply write :
#!/usr/bin/env python
def int2binPadded(number, size):
"""The purpose of this function is to convert integer number to binary
number
0-padded."""
if type(number)!=int or number < 0:
raise ValueError, "should be a
Olivier LEMAIRE wrote:
> I've been looking for 2 days for a way to convert integer to binary number
> 0-padded, nothing... I need to get numbers converted with a defined number
> of bits. For example on 8 bits 2 = 0010 I wrote the following:
> b = str(bin(number))[2:]
The result of bin()
On Wed, Jun 15, 2011 at 10:29 PM, Olivier LEMAIRE
wrote:
> b = str(bin(number))[2:]
> if len(b) !=size:
> b = (size-len(b))*"0"+b
You don't need the str() there as bin() already returns a number.
Here's a relatively trivial simplification - although it does make the
code more cryptic
Hi,
Am 15.06.2011 14:29, schrieb Olivier LEMAIRE:
Hi there, I've been looking for 2 days for a way to convert integer
to binary number 0-padded, nothing... I need to get numbers converted
with a defined number of bits. For example on 8 bits 2 = 0010
bin(2)[2:].zfill(8)
Regards
Daniel
--
Grant Edwards a écrit :
> On 2006-06-02, Bruno Desthuilliers <[EMAIL PROTECTED]> wrote:
>
>>Grant Edwards a écrit :
>>
>>>On 2006-06-01, [EMAIL PROTECTED] <[EMAIL PROTECTED]> wrote:
>>>
does anyone know a module or something to convert numbers like integer
to binary format ?
>>>
>>>They _a
On 2006-06-03, Tim Chase <[EMAIL PROTECTED]> wrote:
>> The fact that they impliment the xor operator is pretty much
>> proof that integers are stored in binary format -- xor is only
>> defined for binary numbers.
>
> Um...let's not use bad logic/proofs for evidencing this...
>
> >>> hasattr(set(),
> The fact that they impliment the xor operator is pretty much
> proof that integers are stored in binary format -- xor is only
> defined for binary numbers.
Um...let's not use bad logic/proofs for evidencing this...
>>> hasattr(set(), "__xor__")
True
:)
-tkc
--
http://mail.python.org/mail
On 2006-06-02, Bruno Desthuilliers <[EMAIL PROTECTED]> wrote:
> Grant Edwards a écrit :
>> On 2006-06-01, [EMAIL PROTECTED] <[EMAIL PROTECTED]> wrote:
>>
>>
>>>does anyone know a module or something to convert numbers like integer
>>>to binary format ?
>>
>>
>> They _are_ in binary format.
>
>
[EMAIL PROTECTED] a écrit :
> Grant Edwards wrote:
>
>>On 2006-06-01, [EMAIL PROTECTED] <[EMAIL PROTECTED]> wrote:
>>
>>
>>>does anyone know a module or something to convert numbers like integer
>>>to binary format ?
>>
>>They _are_ in binary format.
>>
>>
>>>for example I want to convert number 7
Grant Edwards a écrit :
> On 2006-06-01, [EMAIL PROTECTED] <[EMAIL PROTECTED]> wrote:
>
>
>>does anyone know a module or something to convert numbers like integer
>>to binary format ?
>
>
> They _are_ in binary format.
Not really.
>>> (7).__class__
>>> dir((7))
['__abs__', '__add__', '__and_
[EMAIL PROTECTED] a écrit :
> does anyone know a module or something to convert numbers like integer
> to binary format ?
>
> for example I want to convert number 7 to 0111 so I can make some
> bitwise operations...
You don't need to convert anything. The bitwise ops are &, |, <<, >>
0 | 2 | 4
-
Tim Chase <[EMAIL PROTECTED]> wrote:
>bitCount = len([c for c in "01001010101" if c=="1"])
bitCount = "01001010101".count("1")
--
\S -- [EMAIL PROTECTED] -- http://www.chaos.org.uk/~sion/
___ | "Frankly I have no feelings towards penguins one way or the other"
\X/ |-- Arthur C. Clark
[EMAIL PROTECTED] wrote:
> Use the gmpy module.
Yes, it's good. :)
--
http://mail.python.org/mailman/listinfo/python-list
[EMAIL PROTECTED] wrote:
> [EMAIL PROTECTED] wrote:
>
>>does anyone know a module or something to convert numbers like integer
>>to binary format ?
>>
>>for example I want to convert number 7 to 0111 so I can make some
>>bitwise operations...
>>
>>Thanks
>
>
> Use the gmpy module.
>
>
impo
[EMAIL PROTECTED] wrote:
> does anyone know a module or something to convert numbers like integer
> to binary format ?
>
> for example I want to convert number 7 to 0111 so I can make some
> bitwise operations...
>
> Thanks
Use the gmpy module.
>>> import gmpy
>>> a = 14
>>> b = 7
>>> c = 8
>>>
On 2006-06-01, Tim Chase <[EMAIL PROTECTED]> wrote:
for example I want to convert number 7 to 0111 so I can make some
bitwise operations...
>>> Just do it:
>>>
>> 7 & 3
>>> 3
>> 7 | 8
>>> 15
>> I know I can do that but I need to operate in every bit separeted.
>
>
> I suppose ther
On 2006-06-01, [EMAIL PROTECTED] <[EMAIL PROTECTED]> wrote:
does anyone know a module or something to convert numbers like integer
to binary format ?
>>>
>>> They _are_ in binary format.
>>>
for example I want to convert number 7 to 0111 so I can make
some bitwise operations...
On 2006-06-01, [EMAIL PROTECTED] <[EMAIL PROTECTED]> wrote:
>>> does anyone know a module or something to convert numbers like
>>> integer to binary format ?
>>
>> They _are_ in binary format.
>>
>> > for example I want to convert number 7 to 0111 so I can make some
>> > bitwise operations...
>>
>
[EMAIL PROTECTED] wrote:
> Grant Edwards wrote:
> > On 2006-06-01, [EMAIL PROTECTED] <[EMAIL PROTECTED]> wrote:
> >
> > > does anyone know a module or something to convert numbers like integer
> > > to binary format ?
> >
> > They _are_ in binary format.
> >
> > > for example I want to convert num
[EMAIL PROTECTED] wrote:
> does anyone know a module or something to convert numbers like integer
> to binary format ?
>
> for example I want to convert number 7 to 0111 so I can make some
> bitwise operations...
>>> def bits(i,n):
return tuple((0,1)[i>>j & 1] for j in xrange(n-1,-1,-
>>> for example I want to convert number 7 to 0111 so I can make some
>>> bitwise operations...
>> Just do it:
>>
> 7 & 3
>> 3
> 7 | 8
>> 15
> I know I can do that but I need to operate in every bit separeted.
I suppose there might be other operations for which having them
as strings cou
Grant Edwards wrote:
> On 2006-06-01, [EMAIL PROTECTED] <[EMAIL PROTECTED]> wrote:
>
> > does anyone know a module or something to convert numbers like integer
> > to binary format ?
>
> They _are_ in binary format.
>
> > for example I want to convert number 7 to 0111 so I can make some
> > bitwis
En/na [EMAIL PROTECTED] ha escrit:
> does anyone know a module or something to convert numbers like integer
> to binary format ?
http://www.google.es/search?q=python+integer+to+binary
http://aspn.activestate.com/ASPN/Cookbook/Python/Recipe/219300
> for example I want to convert number 7 to 0111
On 2006-06-01, [EMAIL PROTECTED] <[EMAIL PROTECTED]> wrote:
> does anyone know a module or something to convert numbers like integer
> to binary format ?
They _are_ in binary format.
> for example I want to convert number 7 to 0111 so I can make some
> bitwise operations...
Just do it:
>>> 7 &
[EMAIL PROTECTED] schrieb:
> does anyone know a module or something to convert numbers like integer
> to binary format ?
unfortunately there is no builtin function for this
>>> int("111",2)
7
>>> str(7)
'7'
>>> str(7,2)
Traceback (most recent call last):
File "", line 1, in ?
TypeError: str
30 matches
Mail list logo