473,796 Members | 2,640 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

integer to binary...

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

Jun 1 '06 #1
21 3838
ni******@gmail. com 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
Jun 1 '06 #2
On 2006-06-01, ni******@gmail. com <ni******@gmail .com> 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.com yet? It's s'posed to SMOKEY
THE BEAR...
Jun 1 '06 #3
En/na ni******@gmail. com 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/Coo.../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
Jun 1 '06 #4

Grant Edwards wrote:
On 2006-06-01, ni******@gmail. com <ni******@gmail .com> 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.com yet? It's s'posed to SMOKEY
THE BEAR...


Jun 1 '06 #5
ni******@gmail. com 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,-1))
bits(7,4)

(0, 1, 1, 1)

Anton
Jun 1 '06 #6

ni******@gmail. com wrote:
Grant Edwards wrote:
On 2006-06-01, ni******@gmail. com <ni******@gmail .com> 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.com yet? It's s'posed to SMOKEY
THE BEAR...


Jun 1 '06 #7
On 2006-06-01, ni******@gmail. com <ni******@gmail .com> 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.com his "R.V." PAYMENTS are
OVER-DUE!!
Jun 1 '06 #8
>>> 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 "0100101010 1" 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

Jun 1 '06 #9
On 2006-06-01, ni******@gmail. com <ni******@gmail .com> 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.com and toy with a few common
MISAPPREHENSION S...
Jun 1 '06 #10

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
6113
by: David Lawson | last post by:
I know how to conver a string to an array of strings, but I need to convert an ascii string to an array of integers (really unsigned chars). Eg, $str ="ABC"; needs to convert to something like this: $buf = array(0x41, 0x42, 0x43); Anyone know how? I haven't been able to find a way.
0
1562
by: Mark Dufour | last post by:
Hi all, I need to convert an integer into some binary representation. I found the following code in the online cookbook (adapted to return a list): binary = lambda n: n>0 and +binary(n>>1) or This is sure nice, but I'm wondering why something like this doesn't seem to be in the standard library, for example by formatting with '%b' % number. I can't think of a any reason for not doing it this way, as working with binary
6
57570
by: Andrew | last post by:
Hi I have a question is there a function in C++ to convert an integer into a Binary number Thanks in Advance Cheers
17
2273
by: Mantorok Redgormor | last post by:
are all integers represented internally as just bit vectors? -- nethlek
20
9180
by: GS | last post by:
The stdint.h header definition mentions five integer categories, 1) exact width, eg., int32_t 2) at least as wide as, eg., int_least32_t 3) as fast as possible but at least as wide as, eg., int_fast32_t 4) integer capable of holding a pointer, intptr_t 5) widest integer in the implementation, intmax_t Is there a valid motivation for having both int_least and int_fast?
5
2926
by: sathyashrayan | last post by:
Group, I have some doubts in the following program. ------------------program--------------------- /* ** Make an ascii binary string into an integer. */ #include <string.h> unsigned int bstr_i(char *cptr)
3
4159
by: shyha | last post by:
Hello! Does anybody know what is binary representation of integer datatype fields written to archlogs on z/OS (OS/390) machines? Is it "Two's complement", "One's complement", Sign-modulo or whatever? I would analyze it for myself if someone would provide me with some binary (hex) representations of some numbers as: 0, 1, -1 and couple more of your choice.
3
1649
by: David | last post by:
If I have a loop that has a max value of say 20 why would I not want to define the loop counter using a BYTE or SHORT as opposed to a INTEGER. It seems in most books or samples they define fairly small numbers as INTEGER. thanks, David
14
5171
by: Default User | last post by:
Hi, If I have three 64 bit integers and I want to do this operation on them: x*y/z Lets say that what we are multiplying by (y) is offset by what we are dividing by (z) so that the final answer will fit in a 64-bit integer. Let me simplify it by using unsigned chars (8 bits):
0
10449
Oralloy
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10217
jinu1996
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10168
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
10003
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
6785
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5440
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5568
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4114
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
3
2924
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.