Base number

Notation of base

Hexagonal notation When literally expressing a hexadecimal number, it has the following format in Python3. ・ Add "0" at the beginning ・ Write "x" after that ・ Continue to write letters from 0 to 9 and from a to f Alphabetic characters, including the "x", can be either uppercase or lowercase.

If you type a hexadecimal number literal from the keyboard, the converted number will be displayed. Python treats hexadecimal literals as numeric types. Automatically convert hexadecimal literals to decimal numbers. [Example of inputting a hexadecimal literal] 0x1ff → 511

To get a hexadecimal string from a number, use a built-in function called hex (). If a numerical value is given as an argument, a character string equivalent to a hexadecimal number is returned. "Hex" is an abbreviation for "hexadecimal" which means 16 new numbers in English. [Example of converting a decimal number to a character string equivalent to a hexadecimal number] hex (1023) →'ox3ff'

Use the built-in function int () to convert a hexadecimal string to a number instead of a 16m numeric literal. However, pass "16" as the radix to the second argument. [Example of converting a character string equivalent to a hexadecimal number into a numerical value] int (“ox100”, 16) → 256

Binary notation When expressing a binary number literally, Python 3 uses the following format. ・ Add "0" at the beginning ・ Write "b" after that ・ Continue the value of 0 or 1 Like hexadecimal literal notation, binary literals are also converted to decimal numbers. [Example of inputting a binary literal] 0b1000 → 8

To convert a decimal number to a binary equivalent string, use the built-in function bin (). The function name "bin" is an abbreviation for "binary" which means binary notation in English. [Example of converting a decimal number to a character string equivalent to a binary number] bin (1023) → ‘0b1111111111’

To convert a binary equivalent string to an integer, call it by passing the radix "2" as the second argument of the built-in function int ().

8 decimal notation When literally expressing an octal number, Python 3 uses the following format. ・ Add "0" at the beginning ・ Write "o" after that ・ Continue the number from 0 to 7 Until Python2, it was a literal notation of octal numbers using numbers starting from 0, such as "0123". In that case, "0123" is an octal number and "123" is a decimal number, which is confusing, and the notation is different from hexadecimal and binary numbers. With Python3, the notation has been unified. [Example of inputting an octal literal] 0o1777 → 1023

To convert a number to a string equivalent to an octal number, use the built-in function oct (). The function name "oct" is an abbreviation for "octal", which means octal notation in English. [Example of converting a decimal number to a character string equivalent to an octal character string] oct (1023) → '0o1777'

To convert a string equivalent to an octal number to an integer, call it by passing the radix "8" to the second argument of the built-in function int ().

Bit operator Bitwise operators are logical operations that perform binary numbers as if they were individual "bit strings" (columns consisting of 1s and 0s). When you think of operations, you don't think of addition or multiplication, but bit operations are more like logical operations than arithmetic operations such as "+" and "*". A logical operation is an operation that combines comparison operations using "==" with "and" and "or".

Bit operations are used to use a library written in C language, such as when creating programming using something like a GUI library. May be used. Some Python standard libraries have flags to pass as arguments, like the regular expression module (re). Some use bitwise operators when assembling. In Python, you can use special operators to perform bitwise operations on integers. If you want to perform bitwise operations using a string consisting of 0s and 1, you can use the built-in function int () to convert the string to an integer.

[Bitwise operators available in Python]

Bit operator Description
x l y OR the x and y
x & y Take the ethical product (AND) of x and y
x ^ y Take the exclusive OR (XOR) of x and y
x << y , x >> y Shift operator. "<<Shifts x to the left by y bits, ">>Shifts x to y bits to the right

Recommended Posts

Base number
Prime number
Understand base64.
Natural number generator
Base64 decode / encode
Positive number encryption