Re: integer to binary 0-padded

2011-06-18 Thread jmfauth
 '{:+#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


Re: integer to binary 0-padded

2011-06-18 Thread Steven D'Aprano
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 this a question? Or did you just decide to share some code?

Please leave enough context so that readers can understand what you are 
talking about.



-- 
Steven
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: integer to binary 0-padded

2011-06-17 Thread John S
On Jun 15, 9:33 am, Olivier LEMAIRE m.olivier.lema...@gmail.com
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 xrange(10):
... print {0:0{1}b}.format(i,8)
...

0001
0010
0011
0100
0101
0110
0111
1000
1001
-- 
http://mail.python.org/mailman/listinfo/python-list


integer to binary 0-padded

2011-06-15 Thread 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
I wrote the following:
code
#!/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 positive integer
if type(size)!=int:
raise ValueError, should be an integer
if number  (2**size)-1:
raise ValueError, number is too large
# convert int to bin
b = str(bin(number))[2:]

if len(b) !=size:
b = (size-len(b))*0+b

return b


if __name__ == __main__:
import sys
print int2binPadded(int(sys.argv[1]),int(sys.argv[2]))
/code

This satisfies my needs !

Though, what do you think about it ?

Thank you for you remarks,

Olivier
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: integer to binary 0-padded

2011-06-15 Thread Daniel Rentz

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
--
http://mail.python.org/mailman/listinfo/python-list


Re: integer to binary 0-padded

2011-06-15 Thread Chris Angelico
On Wed, Jun 15, 2011 at 10:29 PM, Olivier LEMAIRE
m.olivier.lema...@gmail.com 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:

b = (size*0+bin(number)[2:])[-size:]

After typing this up, I saw Daniel's post, which is rather better. Go
with that one for zero-filling; mine's more general though, you can
pad with other tokens.

ChrisA
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: integer to binary 0-padded

2011-06-15 Thread Peter Otten
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() is already a string, no need to apply str().

 if len(b) !=size:
 b = (size-len(b))*0+b

b.zfill(size) can do that.

 Though, what do you think about it ?

Here's another way (requires Python 2.7):

 {:0{}b}.format(42, 10)
'101010'

-- 
http://mail.python.org/mailman/listinfo/python-list


Re: integer to binary 0-padded

2011-06-15 Thread Olivier LEMAIRE
Thank you to all of you !! 

so finally, I can simply write :
code
#!/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 positive integer
if type(size)!=int:
raise ValueError, should be an integer
if number  (2**size)-1:
raise ValueError, number is too large
# convert int to bin

return bin(number)[2:].zfill(size)


if __name__ == __main__:
import sys
print int2binPadded(int(sys.argv[1]),int(sys.argv[2]))
/code
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: integer to binary 0-padded

2011-06-15 Thread Tim Chase

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


bin(2)[2:].zfill(8)


Just to have in the thread (though the OP seems to be using 2.6 
or later), the bin() function appears to have been added in 2.6 
which is something I wish had been back-ported to 2.4 and 2.5 as 
I've been sufficiently stuck coding against older versions of 
Python to have (re)written a bin() function multiple times.  Ah well.


-tkc



--
http://mail.python.org/mailman/listinfo/python-list


Re: integer to binary 0-padded

2011-06-15 Thread Olivier LEMAIRE
You're right, I use Python 2.6.6 
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: integer to binary...

2006-06-04 Thread Bruno Desthuilliers
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 _are_ in binary format.

Not really.
 
 Yes, really.

No, not really.

  Otherwise the bitwise boolean operations you
 demonstrated wouldn't work as shown.

Ho yes ?

 
(7).__class__

type 'int'

dir((7))

['__abs__', '__add__', '__and__', '__class__', '__cmp__', '__coerce__',
'__delattr__', '__div__', '__divmod__', '__doc__', '__float__',
'__floordiv__', '__getattribute__', '__getnewargs__', '__hash__',
'__hex__', '__init__', '__int__', '__invert__', '__long__',
'__lshift__', '__mod__', '__mul__', '__neg__', '__new__', '__nonzero__',
'__oct__', '__or__', '__pos__', '__pow__', '__radd__', '__rand__',
'__rdiv__', '__rdivmod__', '__reduce__', '__reduce_ex__', '__repr__',
'__rfloordiv__', '__rlshift__', '__rmod__', '__rmul__', '__ror__',
'__rpow__', '__rrshift__', '__rshift__', '__rsub__', '__rtruediv__',
'__rxor__', '__setattr__', '__str__', '__sub__', '__truediv__', '__xor__']

 
 The fact that they impliment the xor operator is pretty much
 proof that integers are

... objects, instance of the int class. Not really what I'd call binary 
format !-)

Now if you go that way, it's of course true that everything on a 
computer ends up in a binary format It's true.

 stored in binary format -- xor is only
 defined for binary numbers.
 
class Prisonner(object):
   def __xor__(self, other):
 return I'm not a (binary) number, I'm a free man

The fact that an object implements the xor operator is pretty much proof 
that the guy that wrote the class decided to implement the xor operator !-)

Grant, I of course agree that, *for practical means*, one can consider 
that Python's integer are already in binary format - for a definition 
of binary format being you can do bitwise ops on them. But the truth 
is that Python integers are objects (in the OO meaning) holding integer 
values - not integer values themselves.
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: integer to binary...

2006-06-03 Thread Grant Edwards
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.

 Not really.

Yes, really.  Otherwise the bitwise boolean operations you
demonstrated wouldn't work as shown.

 (7).__class__
type 'int'
 dir((7))
 ['__abs__', '__add__', '__and__', '__class__', '__cmp__', '__coerce__',
 '__delattr__', '__div__', '__divmod__', '__doc__', '__float__',
 '__floordiv__', '__getattribute__', '__getnewargs__', '__hash__',
 '__hex__', '__init__', '__int__', '__invert__', '__long__',
 '__lshift__', '__mod__', '__mul__', '__neg__', '__new__', '__nonzero__',
 '__oct__', '__or__', '__pos__', '__pow__', '__radd__', '__rand__',
 '__rdiv__', '__rdivmod__', '__reduce__', '__reduce_ex__', '__repr__',
 '__rfloordiv__', '__rlshift__', '__rmod__', '__rmul__', '__ror__',
 '__rpow__', '__rrshift__', '__rshift__', '__rsub__', '__rtruediv__',
 '__rxor__', '__setattr__', '__str__', '__sub__', '__truediv__', '__xor__']


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.

-- 
Grant Edwards   grante Yow!  ... Blame it on the
  at   BOSSA NOVA!!!
   visi.com
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: integer to binary...

2006-06-03 Thread Tim Chase
 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/mailman/listinfo/python-list


Re: integer to binary...

2006-06-03 Thread Grant Edwards
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(), __xor__)
 True

Sets aren't numbers.  Perhaps I should have phrased it better:
xor is only defined for numbers if they are represented in
binary.  If numbers were represented in something other than
binary, then an xor operation on those numbers wouldn't make
sense.

-- 
Grant Edwards   grante Yow!  .. I want to perform
  at   cranial activities with
   visi.comTuesday Weld!!
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: integer to binary...

2006-06-02 Thread Claudio Grondi
[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.
 
 
import gmpy
a = 14
b = 7
c = 8
 
 
help(gmpy.digits)
 
 Help on built-in function digits:
 
 digits(...)
 digits(x[,base]): returns Python string representing x in the
 given base (2 to 36, default 10 if omitted or 0); leading '-'
 present if x0, but no leading '+' if x=0. x must be an mpz,
 or else gets coerced into one.
 
 
print gmpy.digits(a,2)
 
 1110
 
print gmpy.digits(b,2)
 
 111
 
print gmpy.digits(c,2)
 
 1000
 
 
 
help(gmpy.setbit)
 
 Help on built-in function setbit:
 
 setbit(...)
 setbit(x,n,v=1): returns a copy of the value of x, with bit n set
 to value v; n must be an ordinary Python int, =0; v, 0 or !=0;
 x must be an mpz, or else gets coerced to one.
 
 
d = gmpy.setbit(c,1,1)
print gmpy.digits(d,2)
 
 1010
 
 
 
 
help(gmpy.scan1)
 
 Help on built-in function scan1:
 
 scan1(...)
 scan1(x, n=0): returns the bit-index of the first 1-bit of x (that
 is at least n); n must be an ordinary Python int, =0.  If no more
 1-bits are in x at or above bit-index n (which can only happen for
 x=0, notionally extended with infinite 0-bits), None is returned.
 x must be an mpz, or else gets coerced to one.
 
 
help(gmpy.scan0)
 
 Help on built-in function scan0:
 
 scan0(...)
 scan0(x, n=0): returns the bit-index of the first 0-bit of x (that
 is at least n); n must be an ordinary Python int, =0.  If no more
 0-bits are in x at or above bit-index n (which can only happen for
 x0, notionally extended with infinite 1-bits), None is returned.
 x must be an mpz, or else gets coerced to one.
 
 
print gmpy.scan1(a)
 
 1
 
print gmpy.scan1(b)
 
 0
 
print gmpy.scan1(c)
 
 3
 
print gmpy.scan1(d)
 
 1
 
print gmpy.scan0(a)
 
 0
 
print gmpy.scan0(b)
 
 3
 
print gmpy.scan0(c)
 
 0
 
print gmpy.scan0(d)
 
 0
 
 
help(gmpy.popcount)
 
 Help on built-in function popcount:
 
 popcount(...)
 popcount(x): returns the number of 1-bits set in x; note that
 this is 'infinite' if x0, and in that case, -1 is returned.
 x must be an mpz, or else gets coerced to one.
 
 
print gmpy.popcount(a)
 
 3
 
print gmpy.popcount(b)
 
 3
 
print gmpy.popcount(c)
 
 1
 
print gmpy.popcount(d)
 
 2
 
 
 
help(gmpy.hamdist)
 
 Help on built-in function hamdist:
 
 hamdist(...)
 hamdist(x,y): returns the Hamming distance (number of bit-positions
 where the bits differ) between x and y.  x and y must be mpz, or
 else
 get coerced to mpz.
 
 
print gmpy.hamdist(a,b)
 
 2
 
print gmpy.hamdist(a,c)
 
 2
 
print gmpy.hamdist(a,d)
 
 1
 
print gmpy.hamdist(b,c)
 
 4
 
print gmpy.hamdist(b,d)
 
 3
 
print gmpy.hamdist(c,d)
 
 1
 
For those digging deeper into this subject who are looking for speed, 
reading the past discussion on this newsgroup I was part of myself 
looking for fastest way of such integer to binary conversion can maybe 
be of interest:
   http://mail.python.org/pipermail/python-list/2006-January/319295.html
(includes full source code of all compared approaches)

Claudio
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: integer to binary...

2006-06-02 Thread John Salerno
[EMAIL PROTECTED] wrote:

 Use the gmpy module.

Yes, it's good. :)
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: integer to binary...

2006-06-02 Thread Sion Arrowsmith
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. Clarke
   her nu becomeþ se bera eadward ofdun hlæddre heafdes bæce bump bump bump
-- 
http://mail.python.org/mailman/listinfo/python-list

Re: integer to binary...

2006-06-02 Thread Bruno Desthuilliers
[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
- 6
6  2
- 2
2  4
- 32
8  1
- 4


-- 
http://mail.python.org/mailman/listinfo/python-list


Re: integer to binary...

2006-06-02 Thread Bruno Desthuilliers
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__
type 'int'
 dir((7))
['__abs__', '__add__', '__and__', '__class__', '__cmp__', '__coerce__',
'__delattr__', '__div__', '__divmod__', '__doc__', '__float__',
'__floordiv__', '__getattribute__', '__getnewargs__', '__hash__',
'__hex__', '__init__', '__int__', '__invert__', '__long__',
'__lshift__', '__mod__', '__mul__', '__neg__', '__new__', '__nonzero__',
'__oct__', '__or__', '__pos__', '__pow__', '__radd__', '__rand__',
'__rdiv__', '__rdivmod__', '__reduce__', '__reduce_ex__', '__repr__',
'__rfloordiv__', '__rlshift__', '__rmod__', '__rmul__', '__ror__',
'__rpow__', '__rrshift__', '__rshift__', '__rsub__', '__rtruediv__',
'__rxor__', '__setattr__', '__str__', '__sub__', '__truediv__', '__xor__']

-- 
http://mail.python.org/mailman/listinfo/python-list


Re: integer to binary...

2006-06-02 Thread Bruno Desthuilliers
[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 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.

Could you explain the difference ?
-- 
http://mail.python.org/mailman/listinfo/python-list


integer to binary...

2006-06-01 Thread nicolasg
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

-- 
http://mail.python.org/mailman/listinfo/python-list


Re: integer to binary...

2006-06-01 Thread Schüle Daniel
[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 stdin, line 1, in ?
TypeError: str() takes at most 1 argument (2 given)
 

int, str are not symmetrical
I hope this will change in future

rebel on

you can use Ruby's 7.to_s(2) for this
irb(main):001:0 7.to_s(2)
= 111
irb(main):002:0 7.to_s(3)
= 21
irb(main):003:0

/rebel on

 for example I want to convert number 7 to 0111 so I can make some
 bitwise operations...

you can use bitwise operations on int's anyway

7  3 == 3
(1  20) | (1  10) == 2**20+2**10

and so on
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: integer to binary...

2006-06-01 Thread Grant Edwards
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  3
3
 7 | 8
15


-- 
Grant Edwards   grante Yow!  QUIET!! I'm being
  at   CREATIVE!! Is it GREAT
   visi.comyet? It's s'posed to SMOKEY
   THE BEAR...
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: integer to binary...

2006-06-01 Thread Alexis Roda
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 so I can make some
 bitwise operations...

python already provides some bitwise operators:

http://docs.python.org/ref/summary.html



HTH
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: integer to binary...

2006-06-01 Thread nicolasg

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
  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.
 Grant Edwards   grante Yow!  QUIET!! I'm being
   at   CREATIVE!! Is it GREAT
visi.comyet? It's s'posed to SMOKEY
THE BEAR...

-- 
http://mail.python.org/mailman/listinfo/python-list


Re: integer to binary...

2006-06-01 Thread Tim Chase
 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 could be handy.  E.g. counting bits:

bitCount = len([c for c in 01001010101 if c==1])

or parity checking with those counted bits...sure, it can be done 
with the raw stuff, but the operations often tend to be more obscure.

Other reasons for wanting an arbitrary integer in binary might be 
for plain-old-display, especially if it represents bitmap data.

If you just want to operate on each bit, you can iterate over the 
number of bits and shift a single bit to its position:

  target = 10
  shift = 0
  while 1  shift = target:
... print Bit %i is %i % (shift,
... (target  (1  shift))  shift)
... shift += 1
...
Bit 0 is 0
Bit 1 is 1
Bit 2 is 0
Bit 3 is 1


It's ugly, but it works...

-tkc



-- 
http://mail.python.org/mailman/listinfo/python-list


Re: integer to binary...

2006-06-01 Thread Anton Vredegoor
[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)[ij  1] for j in xrange(n-1,-1,-1))

   bits(7,4)
  (0, 1, 1, 1)

Anton
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: integer to binary...

2006-06-01 Thread nicolasg

[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 number 7 to 0111 so I can make some
   bitwise operations...
 
  Just do it:
 
   7  3
  3
   7 | 8
  15
 
 
this is exactly what I need -
http://www.daniweb.com/code/snippet285.html

thanks.
  --
 I know I can do that but I need to operate in every bit separeted.
  Grant Edwards   grante Yow!  QUIET!! I'm being
at   CREATIVE!! Is it GREAT
 visi.comyet? It's s'posed to 
  SMOKEY
 THE BEAR...

-- 
http://mail.python.org/mailman/listinfo/python-list


Re: integer to binary...

2006-06-01 Thread Grant Edwards
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  3
 3
  7 | 8
 15

 I know I can do that but I need to operate in every bit separeted.

Sorry, I've no clue what that means.

-- 
Grant Edwards   grante Yow!  Now KEN is having
  at   a MENTAL CRISIS beacuse
   visi.comhis R.V. PAYMENTS are
   OVER-DUE!!
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: integer to binary...

2006-06-01 Thread Grant Edwards
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  3
 3
  7 | 8
 15

 this is exactly what I need - http://www.daniweb.com/code/snippet285.html

That's nice, but I don't register at web sites like that.

 I know I can do that but I need to operate in every bit
 separeted.

I still don't get what you want a binary string for.

I can see wanting a sequence (e.g. array) of boolean values,
but how are you going to do bitwise operations on a binary
string?

-- 
Grant Edwards   grante Yow!  .. I think I'd
  at   better go back to my DESK
   visi.comand toy with a few common
   MISAPPREHENSIONS...
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: integer to binary...

2006-06-01 Thread Grant Edwards
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 there might be other operations for which having them 
 as strings could be handy.  E.g. counting bits:

 bitCount = len([c for c in 01001010101 if c==1])

 or parity checking with those counted bits...sure, it can be done 
 with the raw stuff, but the operations often tend to be more obscure.

I would think an array or list of bits would be a lot more
useful for doing bitwise operations:

bitCount = sum([0,1,0,0,1,0,1,0,1,0,1])
parity = reduce(operator.xor,[0,1,0,0,1,0,1,0,1,0,1])

 Other reasons for wanting an arbitrary integer in binary might be 
 for plain-old-display, especially if it represents bitmap data.

Yes. I thought C should have had a %b format since the
beginning, but nobody listens. But that's not
what the OP said he wanted it for.  

-- 
Grant Edwards   grante Yow!  Now I'm concentrating
  at   on a specific tank battle
   visi.comtoward the end of World
   War II!
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: integer to binary...

2006-06-01 Thread [EMAIL PROTECTED]

[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

 help(gmpy.digits)
Help on built-in function digits:

digits(...)
digits(x[,base]): returns Python string representing x in the
given base (2 to 36, default 10 if omitted or 0); leading '-'
present if x0, but no leading '+' if x=0. x must be an mpz,
or else gets coerced into one.

 print gmpy.digits(a,2)
1110
 print gmpy.digits(b,2)
111
 print gmpy.digits(c,2)
1000


 help(gmpy.setbit)
Help on built-in function setbit:

setbit(...)
setbit(x,n,v=1): returns a copy of the value of x, with bit n set
to value v; n must be an ordinary Python int, =0; v, 0 or !=0;
x must be an mpz, or else gets coerced to one.

 d = gmpy.setbit(c,1,1)
 print gmpy.digits(d,2)
1010



 help(gmpy.scan1)
Help on built-in function scan1:

scan1(...)
scan1(x, n=0): returns the bit-index of the first 1-bit of x (that
is at least n); n must be an ordinary Python int, =0.  If no more
1-bits are in x at or above bit-index n (which can only happen for
x=0, notionally extended with infinite 0-bits), None is returned.
x must be an mpz, or else gets coerced to one.

 help(gmpy.scan0)
Help on built-in function scan0:

scan0(...)
scan0(x, n=0): returns the bit-index of the first 0-bit of x (that
is at least n); n must be an ordinary Python int, =0.  If no more
0-bits are in x at or above bit-index n (which can only happen for
x0, notionally extended with infinite 1-bits), None is returned.
x must be an mpz, or else gets coerced to one.

 print gmpy.scan1(a)
1
 print gmpy.scan1(b)
0
 print gmpy.scan1(c)
3
 print gmpy.scan1(d)
1
 print gmpy.scan0(a)
0
 print gmpy.scan0(b)
3
 print gmpy.scan0(c)
0
 print gmpy.scan0(d)
0

 help(gmpy.popcount)
Help on built-in function popcount:

popcount(...)
popcount(x): returns the number of 1-bits set in x; note that
this is 'infinite' if x0, and in that case, -1 is returned.
x must be an mpz, or else gets coerced to one.

 print gmpy.popcount(a)
3
 print gmpy.popcount(b)
3
 print gmpy.popcount(c)
1
 print gmpy.popcount(d)
2


 help(gmpy.hamdist)
Help on built-in function hamdist:

hamdist(...)
hamdist(x,y): returns the Hamming distance (number of bit-positions
where the bits differ) between x and y.  x and y must be mpz, or
else
get coerced to mpz.

 print gmpy.hamdist(a,b)
2
 print gmpy.hamdist(a,c)
2
 print gmpy.hamdist(a,d)
1
 print gmpy.hamdist(b,c)
4
 print gmpy.hamdist(b,d)
3
 print gmpy.hamdist(c,d)
1

-- 
http://mail.python.org/mailman/listinfo/python-list