This module should contain operations that can be performed on binary numbers. In the below examples a and b are binary numbers. binary.add(a,b) binary.sub(a,b) binary.mul(a,b) binary.div(a,b) binary.ones_complement(a) //returns 1's complement of a. binary.twos_complement(a) //returns 2's complement of a. binary.dectobi(a) //converts decimal "a" to binary of "a". binary.bcd(a) //returns bcd of a. binary.gray(a) //returns grey code of a. binary.min() //minimizes. binarybitodec //converts binar "a" to decimal of "a".
ananthakrishnan15.2001＠gmail.com wrote:
In the below examples a and b are binary numbers.
Please can you clarify what this means, in Python terms? Are you proposing a _new_ Python type that represents a "binary number", or is `binary.add` (for example) intended to work with existing Python types (for example `int` and `str`)? Who are the intended users of the new functionality, and what would they use it for? Could you perhaps show some example code that might use the module?
And what do you call "decimal numbers"? Decimal representation of numbers like returned by str(123456)? Le dim. 16 févr. 2020 à 15:00, Mark Dickinson <mdickinson@enthought.com> a écrit :
ananthakrishnan15.2001＠gmail.com wrote:
In the below examples a and b are binary numbers.
Please can you clarify what this means, in Python terms? Are you proposing a _new_ Python type that represents a "binary number", or is `binary.add` (for example) intended to work with existing Python types (for example `int` and `str`)?
Who are the intended users of the new functionality, and what would they use it for? Could you perhaps show some example code that might use the module? _______________________________________________ Pythonideas mailing list  pythonideas@python.org To unsubscribe send an email to pythonideasleave@python.org https://mail.python.org/mailman3/lists/pythonideas.python.org/ Message archived at https://mail.python.org/archives/list/pythonideas@python.org/message/VGPNG4... Code of Conduct: http://python.org/psf/codeofconduct/
 Antoine Rozo
I'm proposing a module that has functions intended to work with existing python types. Digital electronics is completely based on "binary number system".As python is used in almost all fields,by adding a seperate module for binary containing operations like ones complement and twos complement (which are much important in digital electronics) ,those who are in the field of digital electronics can make use of this module.
ananthakrishnan15.2001＠gmail.com wrote:
I'm proposing a module that has functions intended to work with existing python types.
Okay, great. *Which* Python types, specifically? `int`? `bytes`? To help us understand, please can you show example inputs to and output from your proposed `binary.add` function, making it clear what the types of the inputs and output are?
Digital electronics is completely based on "binary number system".As python is used in almost all fields,by adding a seperate module for binary containing operations like ones complement and twos complement (which are much important in digital electronics) ,those who are in the field of digital electronics can make use of this module.
16.02.20 17:21, ananthakrishnan15.2001@gmail.com пише:
I'll show the example using one's and two's complement.
binary.ones_complement(11011011110001) 00100100001110 binary.twos_complement(11011011110001) 00100100001111
What is the type of the result of binary.ones_complement() and binary.twos_complement()? It is obviously some new type, because no builtin type has the repr '00100100001110'. What does binary.ones_complement(11011011110002) return?
ananthakrishnan15.2001＠gmail.com wrote:
binary.ones_complement(11011011110001) 00100100001110
I see. So you want `binary.ones_complement` to accept a nonnegative Python `int` whose decimal expansion consists entirely of ones and zeros, interpret that decimal expansion as though it's a bitstring, complement, and then return another such Python `int`. Is that correct? Note that in that case, the output you'd get above would be `100100001110`, not `00100100001110`. Quite apart from whether this is a good idea or not, I can't see how this could even work. Can you please answer the following? 1. What would you expect `ones_complement(1100)` to return? (I'm guessing you'd expect a Python `int` with value `11`.) 2. What about `ones_complement(11111100)`? (I'm guessing that you'd also expect a Python `int` with value `11` here.) 3. What would `ones_complement(ones_complement(1100))` be? 4. What would `ones_complement(ones_complement(11111100))` be?
What would you expect ones_complement(1100) to return? (I'm guessing you'd expect a Python int with value 11.) What about ones_complement(11111100)? (I'm guessing that you'd also expect a Python int with value 11 here.) What would ones_complement(ones_complement(1100)) be? What would ones_complement(ones_complement(11111100)) be? .... we can use 0b1100 instead of 1100.Then the output of binary.twos_complement(0b1100) will be 0b0100
ananthakrishnan15.2001＠gmail.com wrote:
we can use 0b1100 instead of 1100.Then the output of binary.twos_complement(0b1100) will be 0b0100
That would be printed as `4`. Is that what you want? Supposing we accept that `binary.twos_complement(0b1100) == 0b0100`. What would `binary.twos_complement(0b0011)` be equal to? What about `binary.twos_complement(0b00000011)`?
Then can we use a new Python type that represents a "binary number",which accepts number of bits,sign of number.
On Mon, Feb 17, 2020 at 3:40 AM Mark Dickinson <mdickinson@enthought.com> wrote:
ananthakrishnan15.2001＠gmail.com wrote:
binary.twos_complement(0b0011)==1101 binary.twos_complement(0b00000011)==11111101
How would you make that possible, when `0b0011` and `0b00000011` are the exact same integer?
Easy: you simply have 0b1101 and 0b11111101 actually be display phenomena. Both of them represent 3 in a particular field size. Unfortunately the builtin bin() function doesn't take a width argument, but you can easily create that: def binary(n, width=None): if width: return format(n & (1<<width)  1, "0%db" % width) return format(n, "b") Now you can ask for any number's binary representation in a field of N bits, negative numbers included. With that added, you can now use Python's ordinary integers. In fact... import sys sys.displayhook = lambda val: print(binary(val, 8)) if type(val) is int else print(val) Tada! Now your REPL works with eightbit binary values. ChrisA
But there is a problem with (0b1101). 5==0b101 5==0b101 but we want output 5==1011. so this is not possible by using integers with base designator "0b".
But there is a problem with (0b1101). 5==0b101 5==0b101 but we want output 5==1011. so this is not possible by using integers with base designator "0b". so we have to use new Python type that represents a "binary number",which accepts number of bits,sign of number.
On 16 Feb 2020, at 17:49, ananthan ananthan <ananthakrishnan15.2001@gmail.com> wrote:
But there is a problem with (0b1101).
5==0b101 5==0b101
but we want output 5==1011. so this is not possible by using integers with base designator "0b".
There is no such thing as an 'integers with base designator "0b"'. I think are confusing the type that python uses for an integer with the syntax that python allows you to use to specify an integer in your source code. Python considers the follow the same 5 0o5 0x5 0b101
so we have to use new Python type that represents a "binary number",which accepts number of bits,sign of number.
You can write such a type or just mask off the results of the computation with the python integer type. Your example 5 in 4 bits is this
print( bin( 5 & 0b1111 ) ) 0x1011
Exactly what you want? Barry
On Sun, 16 Feb 2020 at 15:34, <ananthakrishnan15.2001@gmail.com> wrote:
I'll show the example using one's and two's complement.
binary.ones_complement(1100111111) 0011000000
But these aren't standard Python types  well, technically, 1100111111 is 1,100,111,111  1 billion, 100 million 111 thousand one hundred and eleven, but I suspect that's not what you intend. And the output  0011000000  is not how Python would print a standard integer type, so either it's a new type (with its own representation) or you're not actually using standard Python types. Are you actually looking for some form of "bit string" type? Paul
On Sun, 16 Feb 2020 at 16:07, <ananthakrishnan15.2001@gmail.com> wrote:
what about
binary.ones_complement(0b1100111111)
or should we use something like "bitstring".
What is 1100111111 here? A number written in decimal where you want to interpret digits as binary digits? Why don't you use 0b1100111111 to have a litteral written in binary representation? Le dim. 16 févr. 2020 à 16:32, <ananthakrishnan15.2001@gmail.com> a écrit :
I'll show the example using one's and two's complement.
binary.ones_complement(1100111111) 0011000000 binary.twos_complement(1100111111) 0011000001
Pythonideas mailing list  pythonideas@python.org To unsubscribe send an email to pythonideasleave@python.org https://mail.python.org/mailman3/lists/pythonideas.python.org/ Message archived at https://mail.python.org/archives/list/pythonideas@python.org/message/RJLGYE... Code of Conduct: http://python.org/psf/codeofconduct/
 Antoine Rozo
On 16 Feb 2020, at 09:38, ananthakrishnan15.2001@gmail.com wrote:
This module should contain operations that can be performed on binary numbers. In the below examples a and b are binary numbers.
Assuming you mean that a "binary number" is int then python can do what you want I think.
binary.add(a,b)
a + b
binary.sub(a,b)
a  b
binary.mul(a,b)
a * b
binary.div(a,b)
a // b
binary.ones_complement(a) //returns 1's complement of a.
~a
binary.twos_complement(a) //returns 2's complement of a.
0  a
binary.dectobi(a) //converts decimal "a" to binary of "a".
If a is an int not sure what you expect this to do.
binary.bcd(a) //returns bcd of a.
Need to know how many bits of a are to be convert then its an easy bit of code to write.
binary.gray(a) //returns grey code of a.
I suspect this to be application specific. Are you thinking Digital TV encoding systems? Is it just a lookup table?
binary.min() //minimizes.
Need a definition of min
binarybitodec //converts binar "a" to decimal of "a".
If a is an int not sure what you expect this to do. Or are you assuming that "binary number" is something else? For example are your binary numbers of a specific bit width? For example an int that only 10 bits are used? In which case can you do the arithmetic operations as above and mask down to the number of bits that you need? Barry
_______________________________________________ Pythonideas mailing list  pythonideas@python.org To unsubscribe send an email to pythonideasleave@python.org https://mail.python.org/mailman3/lists/pythonideas.python.org/ Message archived at https://mail.python.org/archives/list/pythonideas@python.org/message/YIAFNI... Code of Conduct: http://python.org/psf/codeofconduct/
a and b should be integer with base designator "b" (0b110011) .OR there should be a_new_ Python type that represents a "binary number",which accepts number of bits,sign of number.
On Feb 16, 2020, at 05:24, ananthakrishnan15.2001@gmail.com wrote:
This module should contain operations that can be performed on binary numbers.
I think you have two problems here. The first is that you’re confusing integer values with integer literal syntax. It’s not just that `0b101` and `5` are the exact same number, 5, it’s also that all of your operations already make sense for that number 5, and almost all of them are already implemented as operators or methods in Python. So the main part of your question doesn’t even make sense. But there is an additional problem: you want integers fixed to a specific bit length (and signedness). And not even necessarily the native machine lengths 32 and 64; you want to be able to do 3bit arithmetic on 3bit integers. For this, what you want is probably not a module with a bunch of functions on int, but a new class. I’ll call it BinaryInt, and construct it as BinaryInt(n: Integer, *, bits, signed=False), but I think a name that was about being fixedlength instead of about being binary might be less confusing, and of course you can decide on any constructor form you want. (You may also want to be able to take strings, a la decimal.Decimal.) Whether it’s a subclass of int is up to you. The former is more convenient to implement, but subtyping is more important here than implementation. Do you want to be able to pass a BinaryInt(0b101, bits=4) to a function that wants an int and have it act as the integer 3, or to get a TypeError when you try it? (And, if the former, should mypy statically understand that isa relationship?) In fact, there are actually multiple questions here: first, isa int, isnota int but has an __int__ method, or not even that; next, if you’re not an int subtype, you have to decide whether to fit into the numeric tower as an Integer (by subclassing or registering with the ABC); finally, how you want to allow mixing types in operators (whether 1+BinaryInt(0b101, bits=4) should be 6, BinaryInt(0b110, bits=4), or TypeError). You also have to define what happens with overflow and underflow. When you add 4bit unsigned 1110 and 1111, do you get 1101 (modulo, like C), 1111 (saturating), or OverflowError? Is it different for signed? And what happens when you add two numbers of different bit lengths? You can choose whatever str makes sense for your binary ints, but its repr should probably look like the constructor call, probably “normalized”, so `repr(BinaryInt(5, bits=4))` gives you `BinaryInt(0b0101, bits=4)` and `repr(BinaryInt(2, bits=4))` gives you `BinaryInt(0b1110, bits=4))` and `repr(BinaryInt(2, bits=4, signed=True))` gives you `BinaryInt(0b010, bits=4, signed=True)`. Without deciding all of these things, you don’t really have a proposal. But once you do, implementing all the dunder methods is easy, if a bit tedious. (You may want to look at the implementation of Fraction for some useful helper tricks, especially if you want to act like an int when dealing with other types, or even just allow adding different bit lengths.) The handful of things that aren’t builtin operators or methods of int, like Gray coding, you can decide whether to makes these methods on your type or functions. (I think methods make more sense. After all, how can you do gray(8) without specifying it’s supposed to be the 4bit Gray code? But BinaryInt(8, bits=4, unsigned=True).gray() has no such problem.) I doubt this type would be accepted into the stdlib, but even if it isn’t, once you build it, you have it, and you can share it on PyPI for others who want it. Of course first you should search PyPI and make sure it doesn’t already exist. There are definitely modules for bitstrings, fixedpowerof2length ints, and all kinds of other related things, but I don’t know if there’s one for fixedarbitrarylength ints.
On 2/17/20 7:08 AM, ananthan ananthan wrote:
At last found what I was trying to convey.
A new class >>BinaryInt(n: Integer *, bits,signed=False)
It should accept float values.(now >> "bin(5.8)".. will raise an error).
BinaryInt(2,4) 0b1110
I would expect this to be an error, as 'unsigned' binary integers can't be negative, or perhaps accept the Clike wrapping characteristics which would get you the above value. Int(BinaryInt(2,4)) In this case would be 14.
BinaryInt(5,4) 0b0101 BinaryInt(2,4,True) 0b010 I would expect that to be 0b1110, i.e. signed BinaryInts use their MSB as the sign bit, and the others as value bits, not to have a separate sign outside the bits.
 Richard Damon
On Feb 17, 2020, at 04:09, ananthan ananthan <ananthakrishnan15.2001@gmail.com> wrote:
At last found what I was trying to convey.
A new class >>BinaryInt(n: Integer *, bits,signed=False)
It should accept float values.(now >> "bin(5.8)".. will raise an error).
Just float, or any type convertible to int? Whatever you decide, it should be easy to implement. For example, in your __new__ method: # ... # if you only want float, wrap this in an isinstance check intvalue = int(value) # maybe also a try/except/raise around this? if intvalue != value: raise ValueError(f"{cls.__name__} cannot be constructed from nonintegral value {value}") self = int.__new__(cls, intvalue) # ... But just throwing out features you want doesn’t help much. You need to do all the work to design everything, and then implement it. There are a dozen questions along the way (including all the ones I‘ve thought of, but probably more that I haven’t), and building the code is the best way to find all of them, and to have enough context to answer each one. Once you have a complete implementation, you can make the case for why this should be in the stdlib. And, if it gets rejected, you still have it for your own use, and can publish it on PyPI for others. (And maybe, if it gets a lot more use than other people expected, you can propose it to be added to the stdlib again in the future.
It seems to me that this could simply be a package on pypi rather than being added to the Python standard library.
On Thu, Feb 20, 2020 at 01:39 Steve Jorgensen <stevej@stevej.name> wrote:
It seems to me that this could simply be a package on pypi rather than being added to the Python standard library.
Sure. But the interesting part is how to design the API. I’ve seen a number of interesting ideas in this thread.  Guido (mobile)
Guido van Rossum wrote:
On Thu, Feb 20, 2020 at 01:39 Steve Jorgensen stevej@stevej.name wrote:
It seems to me that this could simply be a package on pypi rather than being added to the Python standard library. Sure. But the interesting part is how to design the API. I’ve seen a number of interesting ideas in this thread. Guido (mobile)
Yes. I certainly do agree with that.
participants (12)

ananthakrishnan15.2001＠gmail.com

ananthan ananthan

Andrew Barnert

Antoine Rozo

Barry Scott

Chris Angelico

Guido van Rossum

Mark Dickinson

Paul Moore

Richard Damon

Serhiy Storchaka

Steve Jorgensen