Chapter 1: Problem 19
Write a short C++ function, is TwoPower, that takes an int \(i\) and returns true if and only if \(i\) is a power of \(2 .\) Do not use multiplication or division, however.
Short Answer
Expert verified
Use bitwise operations: ```cppbool isTwoPower(int i) { return (i > 0) && ((i & (i - 1)) == 0); }```
Step by step solution
01
- Understand the Problem
The task is to write a function in C++ that checks if a given integer is a power of 2 without using multiplication or division.
02
- Conceptual Approach
A number is a power of 2 if and only if it has exactly one bit set in its binary representation. For instance, 1 (2^0) is 0001, 2 (2^1) is 0010, 4 (2^2) is 0100, and so on. We can use bitwise operations to check this property.
03
- Use Bitwise AND Operator
If a number is a power of 2, it has only one '1' bit in its binary representation. If we subtract 1 from that number, all the bits after the '1' bit (till the least significant bit) become '1'. For example, 4 in binary is 100, and 3 (4-1) in binary is 011. Therefore, the bitwise AND of these two numbers is zero. Utilizing this property, we can check: if ( i > 0 && (i & (i - 1)) == 0 )
04
- Write the Function
Translate the conceptual approach into a C++ function. The function will accept an integer and return a boolean indicating if the integer is a power of 2. Here is the code:```cppbool isTwoPower(int i) { return (i > 0) && ((i & (i - 1)) == 0);}```
05
- Test the Function
You should test the function with various examples to ensure its correctness.For example:```cppint main() { std::cout << isTwoPower(1); // true std::cout << isTwoPower(2); // true std::cout << isTwoPower(3); // false std::cout << isTwoPower(4); // true std::cout << isTwoPower(5); // false return 0;}```
Unlock Step-by-Step Solutions & Ace Your Exams!
-
Full Textbook Solutions
Get detailed explanations and key concepts
-
Unlimited Al creation
Al flashcards, explanations, exams and more...
-
Ads-free access
To over 500 millions flashcards
-
Money-back guarantee
We refund you if you fail your exam.
Over 30 million students worldwide already upgrade their learning with Vaia!
Key Concepts
These are the key concepts you need to understand to accurately answer the question.
C++ Functions
In C++, a function is a reusable block of code that performs a specific task. Functions help in modularizing code and making it more manageable and readable. Functions are defined using a specific syntax and contain a return type, a function name, a parameter list in parentheses, and a block of code enclosed within curly braces.
Here's the general syntax for a function in C++:
```cpp
returnType functionName(parameter_list) {
// Code to execute
}
```
In the exercise provided, the function `isTwoPower` is designed to take an integer as an input and return a boolean indicating whether the input is a power of two.
Functions provide numerous benefits:
Here's the general syntax for a function in C++:
```cpp
returnType functionName(parameter_list) {
// Code to execute
}
```
In the exercise provided, the function `isTwoPower` is designed to take an integer as an input and return a boolean indicating whether the input is a power of two.
Functions provide numerous benefits:
- Code Reusability: Writing a function once allows it to be reused across the program.
- Easier Maintenance: Changes to the code can be made once within the function, and those changes automatically take effect wherever the function is called.
- Improved Readability: Breaking down complex problems into functions makes code easier to read and understand.
Binary Representation
Binary representation is a way of expressing numbers using only two digits: 0 and 1. It is the foundation of computer systems, as computers operate using binary numbers. Every binary number consists of bits (binary digits), and each bit represents a power of 2.
For example:
By subtracting 1 from a power of 2, the resulting binary number has all bits set to 1 up to the position of the original 1 bit, making it easy to use the bitwise AND operation for verification.
For example:
- The binary number 0001 represents the decimal number 1.
- The binary number 0010 represents the decimal number 2.
- The binary number 0100 represents the decimal number 4.
By subtracting 1 from a power of 2, the resulting binary number has all bits set to 1 up to the position of the original 1 bit, making it easy to use the bitwise AND operation for verification.
Bitwise AND Operator
The Bitwise AND operator is a fundamental bitwise operator in C++ that performs an AND operation on corresponding bits of two integers. It is denoted by the symbol `&`.
When two bits are ANDed:
For example, for the number 4 (binary 100):
Therefore, the condition `(i & (i - 1)) == 0` effectively checks if the number has exactly one bit set.
When two bits are ANDed:
- 1 & 1 results in 1
- 1 & 0 results in 0
- 0 & 0 results in 0
For example, for the number 4 (binary 100):
- 4 - 1 = 3 (binary 011)
- 4 & 3 = 100 & 011 = 000
Therefore, the condition `(i & (i - 1)) == 0` effectively checks if the number has exactly one bit set.
Power of Two
A power of two is a number that can be expressed as 2 raised to an integer exponent `n`. In mathematical form: 2^n 2^n 2^n 2^n Thanks,
Examples include:
Identifying if a number is a power of two can be very useful. As discussed above, we leverage the unique binary representation of these numbers to make the determination easily and efficiently using bitwise operations.
In the `isTwoPower` function, the expression `(i > 0) && ((i & (i - 1)) == 0)` confirms that the number `i` is greater than zero and has only one bit set, ensuring it is a power of two.
Examples include:
- 1 (2^0)
- 2 (2^1)
- 4 (2^2)
- 8 (2^3)
Identifying if a number is a power of two can be very useful. As discussed above, we leverage the unique binary representation of these numbers to make the determination easily and efficiently using bitwise operations.
In the `isTwoPower` function, the expression `(i > 0) && ((i & (i - 1)) == 0)` confirms that the number `i` is greater than zero and has only one bit set, ensuring it is a power of two.