你如何在python上进行二进制加法? [英] How do you do binary addition on python?

查看:788
本文介绍了你如何在python上进行二进制加法?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在尝试正确添加这些内容时遇到了麻烦。我有点碰到了一堵砖墙。我已经把它转换了,就像现在一样。


我的代码:

展开 | 选择 | 换行 | 行号

解决方案

您只需要将十进制整数转换为二进制。然后你的魔法 __ add __ 将是:

展开 | 选择 | Wrap | 行号


我尝试过使用import math的类似代码;并尝试更改它以使其工作,但它没有。


这是我现在的所有代码,用于我的项目:

展开 | 选择 | Wrap | 行号


函数dec2bin()将是一个类方法。查看 此主题

I''m having trouble trying to add these together correctly. I have kind of ran into a brick wall. I have gotten it converted and that''s it as of now.

My Code:

Expand|Select|Wrap|Line Numbers

解决方案

All you need is a conversion of a decimal integer to binary. Then your magic method __add__ would be:

Expand|Select|Wrap|Line Numbers


I have tried a similar code that used "import math" and tried changing it to get it to work, but it didn''t.

This is all of my code as of right now for my project:

Expand|Select|Wrap|Line Numbers


Function dec2bin() would be a class method. Check out this thread.


这篇关于你如何在python上进行二进制加法?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆