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
最满意答案
检查null时,始终使用
==
或!=
。 如果您检查的内容为空,则使用equals
将始终成为NullPointerException
。
==
和equals
之间的区别在于前者检查是否双方指向内存中的相同地址,而后者检查对象是否相等。When checking for null, always use
==
or!=
. Usingequals
will always become aNullPointerException
if what you are checking is null.The difference between
==
andequals
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 ...
-
Java中的hashCode和equals方法之间的关系[duplicate](Relationship between hashCode and equals method in Java [duplicate])[2022-08-09]
您将遇到的问题是根据.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()[duplicate](Overriding hashCode() when overriding equals() [duplicate])[2023-11-02]
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整数总是==吗?(Are Java Integers always == when they're .equals()? [duplicate])[2022-02-27]
当它们是.equals()时,Java整数总是==吗? 不。在正常情况下,尝试进行数值比较时,不能依赖Integer实例。 为此,您必须在比较前将它们解除int ,或者使用equals 。 在您的示例中,您看到的是JDK缓存并重新使用有限数量的Integer实例(对于表示-128至127的Integers )。 来自Integer.valueOf(int) : 此方法将始终缓存-128至127(含)范围内的值,并可缓存此范围之外的其他值。 现在,你的代码正在使用装箱转换而不是Integer.valueOf ... -
与Java泛型相等:子类equals不被称为[duplicate](Equality with Java generics: the subclass equals isn't called [duplicate])[2022-04-15]
我的通灵调试技巧告诉我你没有覆盖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 ... -
java中字符串的==运算符和.equals如何相同?(How are the == operator and .equals for strings in java the same? [duplicate])[2024-03-25]
基本上你看到了字符串实习的结果。 从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 ...