Want to know what Ruby n is the power of 2? (Power judgment of 2)

What is exponentiation in the first place?

To put it simply, is n a number that is a sham of 2? How is it? tell me~~ That No, it ’s really too appropriate and I ’m going to get angry. Click here for details.

https://atarimae.biz/archives/20521

Click here for the judgment formula

bekijyo.rb


if (n&(n-1)) == 0 then
    puts "Power of 2"
else
    puts "Not a power of 2"
end

Background of posting

I didn't know "exponentiation" in the first place, so I made a great detour. It's a uselessly complicated formula, but from the middle "There is a judgment formula that can be solved in one shot." It's already a haguri festival.

That's why it's difficult to go through without knowing the words, It depends on whether you can get there, but if it helps someone.

that's all. I bruised.

Recommended Posts

Want to know what Ruby n is the power of 2? (Power judgment of 2)
What is JSP? ~ Let's know the basics of JSP !! ~
I want to know the answer of the rock-paper-scissors app
[Ruby] I want to reverse the order of the hash table
'% 02d' What is the percentage of% 2?
I want to change the value of Attribute in Selenium of Ruby
What is testing? ・ About the importance of testing
What to do when you want to know the source position where the method is defined in binding.pry
What is the data structure of ActionText?
I want to know the JSP of the open portlet when developing Liferay
[Ruby] I want to extract only the value of the hash and only the key
What is CHECKSTYLE: OFF found in the Java source? Checkstyle to know from
How to find the cause of the Ruby error
The story of introducing Ajax communication to ruby
I want to var_dump the contents of the intent
[Ruby] Code to display the day of the week
I want to get the value in Ruby
[Ruby] Meaning of &. How to avoid the error when the receiver (object) is nil
What is ... (3 dots) found in the Java source? Variadic arguments to know from
[Ruby] I want to make a program that displays today's day of the week!
When the hover of Eclipse is hard to see
If you want to know the options when configuring Ruby, see `RbConfig :: CONFIG ["configure_args "]`
Judgment of the calendar
I want to display the name of the poster of the comment
I want to be aware of the contents of variables!
I want to return the scroll position of UITableView!
[Ruby] What is true?
I want to find out which version of java the jar file I have is available
I tried to solve the Ruby karaoke machine problem (there is an example of the answer)
I tried to solve the Ruby bonus drink problem (there is an example of the answer)
Review the basic knowledge of ruby that is often forgotten
Determine that the value is a multiple of 〇 in Ruby
I want to expand the clickable part of the link_to method
I want to change the log output settings of UtilLoggingJdbcLogger
What is the representation of domain knowledge in the [DDD] model?
The nth and n + 1st characters of a Ruby string
What I did in the version upgrade from Ruby 2.5.2 to 2.7.1
[Java] To know the type information of type parameters at runtime
I tried to summarize the basic grammar of Ruby briefly
[Ruby] How to retrieve the contents of a double hash
The latest technology that busy engineers want to know roughly
Road to Java Engineer Part2 What kind of language is Java?
When you want to change the MySQL password of docker-compose
What is an immutable object? [Explanation of how to make]
I want to narrow down the display of docker ps
I want to temporarily disable the swipe gesture of UIPageViewController
What is a Ruby module?
What is the pluck method?
What is the BufferedReader class?
[Ruby] What is `!!` used for?
[Ruby] What is an instance?
Judgment of fractions in Ruby
What is the constructor for?
Introduction to Ratpack (1) --What is Ratpack?
What is the initialize method?
[Swift] When you want to know if the number of characters in a String matches a certain number ...
I want to solve the N + 1 problem where the data is collated excessively and the operation becomes heavy.
The story of making a binding for libui, a GUI library for Ruby that is easy to install
[Note] What to do if bundle install in Chapter 3 of the rails tutorial is not possible
I tried to solve the Ruby bingo card creation problem (there is an example of the answer)
I want to make the frame of the text box red when there is an input error