- StudyBlue
- Alabama
- University of Alabama - Tuscaloosa
- Computer Science
- Computer Science 150
- Wiegand
- Bit-string Operations on Integer Types¶

charles h.

x | y

bitwise or of x and y

x & y

bitwise and of x and y

Advertisement

x << n

x shifted left by n bits

x >> n

x shifted right by n bits

~x

the bits of x inverted

int.bit_length()

Return the number of bits necessary to represent an integer in binary, excluding the sign and leading zeros:

float.as_integer_ratio()¶

Return a pair of integers whose ratio is exactly equal to the original float and with a positive denominator. Raises OverflowError on infinities and a ValueError on NaNs.

float.hex()¶

Return a representation of a floating-point number as a hexadecimal string. For finite floating-point numbers, this representation will always include a leading 0x and a trailing p and exponent.

float.fromhex(s)¶

Class method to return the float represented by a hexadecimal string s. The string s may have leading and trailing whitespace.

Iterator Types¶

Python supports a concept of iteration over containers. This is implemented using two distinct methods; these are used to allow user-defined classes to support iteration. Sequences, described below in more detail, always support the iteration methods.

container.__iter__()¶

Return an iterator object. The object is required to support the iterator protocol described below. If a container supports different types of iteration, additional methods can be provided to specifically request iterators for those iteration types. (An example of an object supporting multiple forms of iteration would be a tree structure which supports both breadth-first and depth-first traversal.) This method corresponds to the tp_iter slot of the type structure for Python objects in the Python/C API.

Advertisement

iterator.__iter__()¶

Return the iterator object itself. This is required to allow both containers and iterators to be used with the for and in statements. This method corresponds to the tp_iter slot of the type structure for Python objects in the Python/C API.

iterator.__next__()¶

Return the next item from the container. If there are no further items, raise the StopIteration exception. This method corresponds to the tp_iternext slot of the type structure for Python objects in the Python/C API.

Bit-string Operations on Integer Types¶

Integers support additional operations that make sense only for bit-strings. Negative numbers are treated as their 2?s complement value (this assumes a sufficiently large number of bits that no overflow occurs during the operation).

The priorities of the binary bitwise operations are all lower than the numeric operations and higher than the comparisons; the unary operation ~ has the same priority as the other unary numeric operations (+ and -).

The priorities of the binary bitwise operations are all lower than the numeric operations and higher than the comparisons; the unary operation ~ has the same priority as the other unary numeric operations (+ and -).

Want to see the other 14 Flashcards in Bit-string Operations on Integer Types¶?
JOIN TODAY FOR FREE!

"The semester I found StudyBlue, I went from a 2.8 to a 3.8, and graduated with honors!"

Jennifer Colorado School of Mines
StudyBlue is not sponsored or endorsed by any college, university, or instructor.

© 2015 StudyBlue Inc. All rights reserved.

© 2015 StudyBlue Inc. All rights reserved.