Programming to fight in the world 5-3

Bit operation is difficult ... Especially when it comes to writing in programming ...

bit.py


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

class Bitutils():
        
    def getNextArith(self,n):
        c = n
        c0 = 0
        c1 = 0
        while c & 1 == 0 and c != 0:
            c0 += 1
            c >>= 1
        
        while c & 1 == 1:
            c1 += 1
            c >>= 1
        if (c0 + c1 == 31 or c0 + c1 == 0):
            return -1
        return n + (1 << c0) + (1 << (c1 -1)) -1
    
    def getPrevArith(self,n):
        temp = n
        c0 = 0
        c1 = 0
        while temp & 1 == 1:
            c1 += 1
            temp >>= 1
            
        if temp == 0:
            return -1
        
        while temp & 1 == 0 and temp != 0:
            c0 += 1
            temp >>= 1
        
        
        return n - (1 << c1) - (1<<(c0 - 1)) + 1

I will think again after taking a bath.

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.
"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
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.