Monthly Archives: February 2024

Decimal numbers to other integer base

A decimal number can be easily converted to any other integer using the method of repeated division.

Repeated Division

  • Divide the decimal number by the desired base. Write down the integer quotient and keep the remainder.
  • Convert the remainder to its equivalent in the new base (0-9 for bases 2-10, then A-F, etc. for higher bases).
  • Repeat steps 1 and 2 with the quotient, treating it as a new decimal number. Stop when the quotient becomes 0.
  • Read the remainders in reverse order. This will be your number in the new base.

Example1: Convert 255 to base 8 (octal).

  • 255 ÷ 8 = 31, remainder 7
  • 31 ÷ 8 = 3, remainder 7
  • 3 ÷ 8 = 0, remainder 3

Therefore, 255 in octal is 377.

Example2: Convert 255 to hexadecimal.

  • 255 ÷ 16 = 15, remainder 15 (F)
  • 15 ÷ 16 = 0, remainder 15 (F)

Therefore, 255 in hexadecimal is FF.

Python Global Interpreter Lock (GIL)

Some notes on Python’s GIL:

  1. It is basically a mechanism in the CPython interpreter (the most common Python implementation) that ensures only one thread can execute Python bytecode at a time.
  2. The GIL prevents multiple threads from modifying reference counts at the same time, which could lead to memory corruption.
  3. This limits the performance gains you can achieve with multithreading for CPU-intensive tasks.
  4. But threads often spend time waiting for I/O operations (like network requests or file access), which can allow other threads to run even with the GIL. So, multithreading can still be beneficial for I/O-bound tasks.
  5. If you need true parallelism for CPU-bound tasks, use the multiprocessing module to create multiple processes, each with its own interpreter and GIL.
  6. Some libraries, like NumPy and Cython, release the GIL during certain operations, allowing better performance for multithreaded code.
  7. Alternative Python implementations like Jython and IronPython don’t have a GIL, but they have their own limitations and trade-offs.