Linked Questions
55 questions linked to/from How to check if a number is a power of 2
19 votes
2 answers
15k views
Test if a bitboard have only one bit set to 1 [duplicate]
I have a bitboard and I want to check in C if there is ONLY one bit set to 1. #include <stdint.h> typedef uint64_t bboard; bboard b = 0x0000000000000010; if (only_one_bit_set_to_one (b)) // in ...
7 votes
4 answers
18k views
Determine if an int is a power of 2 or not in a single line [duplicate]
Possible Duplicate: How to check if a number is a power of 2 I made the following code, but it's not working. The compiler gives an error that for a missing ) and expression syntax error. What is ...
5 votes
9 answers
40k views
Determine if number is in the binary sequence 1 2 4 8 16 32 64 etc [duplicate]
Possible Duplicate: How to check if a number is a power of 2 I want to determine if a number is in 1 2 4 8 16 32 64 128 256 512 1024 2048 4096 8192 16384 ... I tried this: public static void ...
0 votes
2 answers
4k views
Check if a number is 2^n [duplicate]
Possible Duplicate: How to check if a number is a power of 2 This question has been asked in an interview. How to check if a number is in 2^n format {1, 2, 4, 8, 16, 32, ....} without using *, ...
1 vote
2 answers
10k views
Determine if num is a power of two in java? [duplicate]
Possible Duplicate: How to check if a number is a power of 2 How could I write a method that would return true if passed in the value 2, 4, 8, 32, 64, and so on?
5 votes
2 answers
730 views
Evaluate if integer is POT (Power Of Two) [duplicate]
Possible Duplicates: Query about working out whether number is a power of 2 How to check if a number is a power of 2 I require a function body for this prototype: bool isPOT(int x); So it ...
2 votes
1 answer
637 views
fastest way to find a given number 'n' can be absolutely expressed as 2^m [duplicate]
Possible Duplicate: How to check if a number is a power of 2 fastest way to find a given number 'n' can be expressed as 2^m ex: 16= 2^4 naive solution: divide given number by 2 until the ...
-1 votes
3 answers
230 views
Is there any faster way to check if a number is a integral power of 2 in c++ [duplicate]
I know this method and it is not efficient enough (a/2)%2==0;
0 votes
1 answer
2k views
Check if a bitmask consists of multiple flags [duplicate]
Note, this is not a dupe of this question: Testing if a bitmask has one and only one flag I need to validate whether or not a bitmask consists of multiple flags. I've come up with this method, but ...
3 votes
1 answer
1k views
Most efficient way to check if only one '1' exists in binary representation of a decimal number [duplicate]
What is the most efficient (in terms of speed and space) way to check if a decimal number only has a single '1' in its binary representation without using special functions (math, numpy, etc.)? e.g. ...
-3 votes
3 answers
977 views
determine whether an int is power of 2 [duplicate]
public class test{ public static void main(String[] args) { int a=536870912; System.out.print((Math.log(a)/Math.log(2))); } } 536870912 is a number that is power of two, but the result is ...
0 votes
1 answer
507 views
Check number of processes in MPI for power of two [duplicate]
Sorry if this is a stupid question but I just want to know whats the actual meaning of the if statement below. int rank, numprocs; MPI_Status status; MPI_Init(&argc,&argv); MPI_Comm_size( ...
-3 votes
2 answers
125 views
Is there a trick to testing if a non-specific bit is set anywhere in an int? [duplicate]
I want to test whether any single bit is set in a C int. I don't care which bit it is. Equivalent to if (1 == count_bits (n)) I'm curious if there is a non-looping algorithm for this which does not ...
-3 votes
2 answers
78 views
Determine if sum is a power of 2 [duplicate]
What is the fastest (in asymptotic worst-case time complexity) algorithm for determining if a sum of arbitrary positive integers is a power of two?
0 votes
1 answer
93 views
shortest way to check if a positive integer is a power of two [duplicate]
This question is from job interview...Use this template to write a C++ function that checks if a positive integer is a power of two. bool p(int n) { return ********; } You have to replace the 8 '*...