Snippet when searching all bits with python

I want to search all bits with python

A note for yourself. If you feel like it, rewrite it properly.

Sometimes you want to convert a decimal number x to an n-digit binary number. (Mainly when searching all bits) You can use format at that time, but I couldn't remember how to write it, so Make a note of how to use it often.

Explanation of the code for when you forget. The binary number of x with bin (x >> i) is shifted by one digit. I want only the last digit of the shifted number, so I calculate the remainder after dividing by 2. Since it is necessary to return the bit-shifted one to int in order to calculate the remainder divided by 2. As ʻint (bin, 2) , the binary number is converted back to the decimal number. If you change the order of assigning to x_array_bin, you can make it properly [0,0,0,1,1] `, Since it is not so relevant in the bit full search, it is left as it is.


x = 3
n = 5
x_array_bin = [0 for _ in range(n)]
for i in range(n):
        x_array_bin[i] = int(bin(x >> i),2)%2

print(x_array_bin)

#[1, 1, 0, 0, 0]
    

Recommended Posts

Snippet when searching all bits with python
python snippet collection with cinema4d
Error when playing with python
When matplotlib doesn't work with python2.7
When using MeCab with virtualenv python
Precautions when using six with Python 2.5
[Python] Format when to_csv with pandas
python snippet
Note when creating an environment with python
Precautions when solving DP problems with Python
FizzBuzz with Python3
Scraping with Python
Recommended environment and usage when developing with Python
Statistics with python
Scraping with Python
Python with Go
Personal tips when doing various things with Python 3
Twilio with Python
Integrate with Python
Play with 2016-Python
Precautions when dealing with control structures in Python 2.6
AES256 with python
Tested with Python
python starts with ()
Investigation when import cannot be done with python
A memo when creating a python environment with miniconda
Character encoding when dealing with files in Python 3
with syntax (Python)
[python] [vscode] When you get angry with space-tab-mixed
Bingo with python
Zundokokiyoshi with python
[Web development with Python] Precautions when saving cookies
Materials to read when getting started with Python
Excel with Python
Microcomputer with Python
What are you using when testing with Python?
[Python] Customize Colormap when drawing graphs with matplotlib
Cast with python
BigQuery-Python was useful when working with BigQuery from Python
Findings when accelerating numerical calculations with Python and Numba
Precautions when dealing with ROS MultiArray types in Python
Mailbox selection when retrieving Gmail with imaplib in python
Problems when creating a csv-json conversion tool with python
Troublesome story when using Python3 with VScode on ubuntu
Tips you should know when programming competitive programming with Python2
Japanese output when dealing with python in visual studio
Directory structure when writing tests with Python 3 standard unittest
Serial communication with Python
Zip, unzip with python
Django 1.11 started with Python3.6
Primality test with Python
Python with eclipse + PyDev.
Socket communication with Python
Data analysis with python 2
Scraping with Python (preparation)
Learning Python with ChemTHEATER 03
Sequential search with Python
Run Python with VBA
Handling yaml with python
Solve AtCoder 167 with python
Serial communication with python