Programming to fight in the world ~ 5-2

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,nim,i,v):
        mask = ~(1 << i)
        return (num & mask) | (v << i)
    
    def updateBit(self,n,m,i,j): #5-1
        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
    
    def printBinary(self,num): #5-2
        if num >= 1 or num <= 0:
            return "Error"
        tmp_str = ""
        tmp_str += "0."
        
        while num > 0:
            if len(tmp_str) >= 33:
                return "Error"
            r = num * 2
            if r >= 1:
                tmp_str += "1"
                num = r - 1
            else:
                tmp_str+= "0"
                num = r
                
        return tmp_str
            
            
            
    

if __name__ == '__main__':
    N = 1024
    M = 19
    i = 2
    j = 6
    b = Bitutils()
    
    #5-1
    #c = b.updateBit(N,M,i,j)
    #print(bin(c))
    
    #5-2
    c = b.printBinary(0.75)
    print(c)
    
    c = b.printBinary(0.875)
    print (c)

Is this all right in python when adding new characters to an existing string? Does this mean that we are creating a completely new string?

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
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
"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
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
"A book to train programming skills to fight in the world" Python code answer example --1.2 Count the number of the same characters
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
How to use the exists clause in Django's queryset
I want to write in Python! (3) Utilize the mock
I used Python to find out about the role choices of the 51 "Yachts" in the world.
The road to Pythonista
Python programming in Excel