首页 \ 问答 \ Python:选择代表二进制数的位数(Python: Choose number of bits to represent binary number)

Python:选择代表二进制数的位数(Python: Choose number of bits to represent binary number)

我很好奇如何更改位数以表示二进制数。 例如,假设我想要将十进制1表示为二进制。 我用:

bin(1)并获得0b1。

如何获得返回说0b01或0b001或0b0001等?


I am curious as to how I can change the number of bits to represent a binary number. For example, say I want express decimal 1 to binary. I use:

bin(1) and get 0b1.

How can I get the return to say 0b01 or 0b001 or 0b0001 etc?


原文:https://stackoverflow.com/questions/13676183
更新时间:2023-07-07 18:07

最满意答案

检查null时,始终使用==!= 。 如果您检查的内容为空,则使用equals将始终成为NullPointerException

==equals之间的区别在于前者检查是否双方指向内存中的相同地址,而后者检查对象是否相等。


When checking for null, always use == or !=. Using equals will always become a NullPointerException if what you are checking is null.

The difference between == and equals is that the former checks whether or not both sides points to the same address in memory, while the latter checks for object equality.

相关问答

更多
  • 你不能重载==操作符,但是如果你希望它与==操作符的行为不同,即不比较引用,但实际上比较对象(例如使用它们的全部或部分字段),你可以重写equals(Object) )。 另外,如果你重写了equals(Object) ,那么看看hashCode() 。 这两个方法需要兼容(即根据equals(Object)需要具有相同的hashCode()两个对象相同),否则会出现各种奇怪的错误(例如,将对象添加到集合或映射时) 。 You can not overload the == operator, but yo ...
  • 在Java中, ==总是只比较两个引用(对于非原语,也就是) - 即它测试两个操作数是否引用同一个对象。 但是, equals方法可以被覆盖 - 所以两个不同的对象仍然可以相等。 例如: String x = "hello"; String y = new String(new char[] { 'h', 'e', 'l', 'l', 'o' }); System.out.println(x == y); // false System.out.println(x.equals(y)); // true ...
  • 您将遇到的问题是根据.equals()和.hashCode() ,例如HashMap键计算元素的一致性的集合。 顾名思义,它依赖于哈希表,哈希桶是对象的.hashCode()的函数。 如果你有两个对象是.equals() ,但有不同的哈希码,你输了! 这里重要的一部分合约是: .equals()必须具有相同的.hashCode() 。 这一切都记录在javadoc for Object 。 而约书亚布洛赫说,你必须在有效的Java中做到这一点。 说够了。 The problem you will have ...
  • 您说您已经知道哪个自定义类需要覆盖hashCode / equals? 然后您还知道哪些属性(全局变量)确定每个类的相等性。 当您了解这些属性时,您可以手动或通过使用现代IDE(如Eclipse,NetBeans等)生成方法来实现hashCode / equals。在Eclipse中,在“源”下有一个名为“Generate hashCode()和equals()”的选项“菜单 You say you already know which one of your custom classes need to ...
  • equals()和hashCode()在某些集合(如HashSet和HashMap hashCode()中被联合使用,因此您必须确保如果使用这些集合,则根据合约覆盖hashCode 。 如果你根本没有重写hashCode ,那么你会遇到HashSet和HashMap问题。 特别地,两个“相等”的对象可以放在不同的散列桶中,即使它们应该相等。 如果你重写hashCode ,但做得不好,那么你会遇到性能问题。 您所有的HashSet和HashMap条目都将放入同一个存储桶中,您将失去O(1)性能,并且具有O(n ...
  • 你应该自己进行类型检查,只是像你说的那样转换不安全,因为它可能是错误的类型,然后它将抛出ClassCastException 。 使用对象的原因是你可能有不同的类可能是相同的(虽然这很少见),如果你不覆盖equals的对象形式,那么当你使用集合时你会遇到问题,因为事情不会是等于应该是,因为集合将使用对象形式。 从理论上讲,可能存在通用的equals接口,但equals方法早于遗传学,并且能够用任何其他对象检查任何对象简化了大量代码。 You should do the type check yourself ...
  • 检查null时,始终使用==或!= 。 如果您检查的内容为空,则使用equals将始终成为NullPointerException 。 ==和equals之间的区别在于前者检查是否双方指向内存中的相同地址,而后者检查对象是否相等。 When checking for null, always use == or !=. Using equals will always become a NullPointerException if what you are checking is null. The di ...
  • 当它们是.equals()时,Java整数总是==吗? 不。在正常情况下,尝试进行数值比较时,不能依赖Integer实例。 为此,您必须在比较前将它们解除int ,或者使用equals 。 在您的示例中,您看到的是JDK缓存并重新使用有限数量的Integer实例(对于表示-128至127的Integers )。 来自Integer.valueOf(int) : 此方法将始终缓存-128至127(含)范围内的值,并可缓存此范围之外的其他值。 现在,你的代码正在使用装箱转换而不是Integer.valueOf ...
  • 我的通灵调试技巧告诉我你没有覆盖Token的基本equals()方法,就像你没有在Node 。 您需要将其声明为equals(Object) 。 添加@Override注释以捕获它。 My psychic debugging skills tell me that you didn't override the base equals() method in Token, just like you didn't in Node. You need to declare it as equals(Objec ...
  • 基本上你看到了字符串实习的结果。 从JLS第15.28节开始 : String类型的编译时常量表达式总是“实例化”,以便使用String.intern方法共享唯一实例。 所以你的两个变量值实际上是指相同的字符串。 如果您使用: String s = new String("Hello"); String x = new String("Hello"); ...然后你会看到你期望的行为。 Basically you're seeing the result of string interning. From ...

相关文章

更多

最新问答

更多
  • h2元素推动其他h2和div。(h2 element pushing other h2 and div down. two divs, two headers, and they're wrapped within a parent div)
  • 创建一个功能(Create a function)
  • 我投了份简历,是电脑编程方面的学徒,面试时说要培训三个月,前面
  • PDO语句不显示获取的结果(PDOstatement not displaying fetched results)
  • Qt冻结循环的原因?(Qt freezing cause of the loop?)
  • TableView重复youtube-api结果(TableView Repeating youtube-api result)
  • 如何使用自由职业者帐户登录我的php网站?(How can I login into my php website using freelancer account? [closed])
  • SQL Server 2014版本支持的最大数据库数(Maximum number of databases supported by SQL Server 2014 editions)
  • 我如何获得DynamicJasper 3.1.2(或更高版本)的Maven仓库?(How do I get the maven repository for DynamicJasper 3.1.2 (or higher)?)
  • 以编程方式创建UITableView(Creating a UITableView Programmatically)
  • 如何打破按钮上的生命周期循环(How to break do-while loop on button)
  • C#使用EF访问MVC上的部分类的自定义属性(C# access custom attributes of a partial class on MVC with EF)
  • 如何获得facebook app的publish_stream权限?(How to get publish_stream permissions for facebook app?)
  • 如何防止调用冗余函数的postgres视图(how to prevent postgres views calling redundant functions)
  • Sql Server在欧洲获取当前日期时间(Sql Server get current date time in Europe)
  • 设置kotlin扩展名(Setting a kotlin extension)
  • 如何并排放置两个元件?(How to position two elements side by side?)
  • 如何在vim中启用python3?(How to enable python3 in vim?)
  • 在MySQL和/或多列中使用多个表用于Rails应用程序(Using multiple tables in MySQL and/or multiple columns for a Rails application)
  • 如何隐藏谷歌地图上的登录按钮?(How to hide the Sign in button from Google maps?)
  • Mysql左连接旋转90°表(Mysql Left join rotate 90° table)
  • dedecms如何安装?
  • 在哪儿学计算机最好?
  • 学php哪个的书 最好,本人菜鸟
  • 触摸时不要突出显示表格视图行(Do not highlight table view row when touched)
  • 如何覆盖错误堆栈getter(How to override Error stack getter)
  • 带有ImageMagick和许多图像的GIF动画(GIF animation with ImageMagick and many images)
  • USSD INTERFACE - > java web应用程序通信(USSD INTERFACE -> java web app communication)
  • 电脑高中毕业学习去哪里培训
  • 正则表达式验证SMTP响应(Regex to validate SMTP Responses)