[PYTHON] Programming to fight in the world ~ 5-1

bit.py


#-*- coding:utf-8 -*-

class Bitutils():

    
    def getBit(self,num,i):
        return(num & (1 << i) !=0 )
    
    def setBit(self,num1,i):
        return (num | (1 << i))
    
    def clearBit(self,num,i):
        mask = ~(1 << i)
        return num & mask
    
    def clearBitsMSBthroughI(self,num,i):
        mask = (1 << i) -1
        return num & mask
    
    def clearBitsIthrough(self,num,i):
        mask = ~((1 << (i+1)) -1)
    
    def updateBit(self,n,m,i,j):
        allOnes = ~0
        
        left = allOnes << (j + 1)
        
        right = ((1<<i) - 1)
        
        mask = left|right
        
        n_cleared = n & mask
        m_shifted = m << i
        
        return n_cleared | m_shifted
    

if __name__ == '__main__':
    N = 1024
    M = 19
    i = 2
    j = 6
    b = Bitutils()
    c = b.updateBit(N,M,i,j)
    print(bin(c))

Bit shift and or operation was easy, but For insertion, you have to set the insertion part to 0 once.

Recommended Posts

Programming to fight in the world ~ 5-1
Programming to fight in the world ~ 5-5,5-6
Programming to fight in the world 5-3
Programming to fight in the world ~ 5-2
Programming to fight in the world-Chapter 4
"Book to train programming skills to fight in the world" Python code answer example --1.3 URLify
"Book to train programming skills to fight in the world" Python code answer example --2.6 palindrome
"Book to train programming skills to fight in the world" Python code answer example --2.4 Splitting the list
"Book to train programming skills to fight in the world" Python code answer example --2.7 intersecting nodes
"A book to train programming skills to fight in the world" Python code answer example --1.8 "0" matrix
"A book to train programming skills to fight in the world" Python code Solution example --1.6 String compression
"A book to train programming skills to fight in the world" Python code solution example --1.5 One-shot conversion
"A book to train programming skills to fight in the world" Python code answer example --3.1 Three stacks
"A book to train programming skills to fight in the world" Python code Solution example --1.7 Matrix rotation
"Book to train programming skills to fight in the world" Python code answer example --1.4 Permutation of sentences
"A book to train programming skills to fight in the world" Python code Solution example --2.8 Loop detection
Programming language in "Hello World"
"Book to train programming skills to fight in the world" Python code solution example --- Removed elements between 2.3
"Book to train programming skills to fight in the world" Python code Solution example --2.1 Remove duplicate elements
The easiest line bot in the world to lose weight
"A book to train programming skills to fight in the world" Python code answer example --1.9 Rotation of strings
"A book to train programming skills to fight in the world" Python code answer example --1.1 Duplicate character string
Activity record in the programming circle
In the python command python points to python3.8
The most cited patent in the world
"A book to train programming skills to fight in the world" Python code answer example --2.2 Returns the Kth from the back
Cython to try in the shortest
"A book to train programming skills to fight in the world" Python code answer example --1.2 Count the number of the same characters
Hit the echo command in the Mac terminal to output Hello World
I want to know the population of each country in the world.
The most sought after programming language in 2020
"A book to train programming skills to fight in the world" Python code Solution example --2.5 The sum of two numbers shown in the list
In Jupyter, add IPerl to the kernel.
Draw graphs in the programming language Julia
Various comments to write in the program
How to display Hello world in python
Guidelines for reincarnating in the world of linux programming development (C / C ++ language)
Programming in python
How to use the C library in Python
Log in to the remote server with SSH
Twitter streaming client to enjoy in the terminal
To dynamically replace the next method in python
Say hello to the world with Python with IntelliJ
Draw graphs in Julia ... Leave the graphs to Python
The trick to write flatten concisely in python
How to get the files in the [Python] folder
Use pygogo to get the log in json.
I want to display the progress in Python!
How to retrieve the nth largest value in Python
I tried to graph the packages installed in Python
How to get the variable name itself in python
Try to solve the programming challenge book with python3
How to run the Ansible module added in Ansible Tower
The easiest way to set up Last-Modified in Flask
Add auto-completion to EV3 Micropyhon programming in VS Code
How to get the number of digits in Python
The story of building the fastest Linux environment in the world
How to know the current directory in Python in Blender
If there were no DI containers in the world.
Convert the image in .zip to PDF with Python
Set the form DateField to type = date in Django