Given a positive integer **N**, the task is to count the numbers that can be represented with **N** bits and whose **0 ^{th}** and

**N**bits are set.

^{th}**Examples:**

Input:N = 2

Output:1

All possible 2-bit integers are 00, 01, 10 and 11.

Out of which only 11 has 0^{th}and N^{th}bit set.

Input:N = 4

Output:4

**Approach:** Out of the given **N** bits, only two bits need to be set i.e. the **0 ^{th}** and the

**N**bit. So, setting these 2 bits as 1 we are left with the rest

^{th}**N – 2**bits every single of which can either be

**0**or

**1**and there are

**2**ways of doing that.

^{N – 2}Below is the implementation of the above approach:

## C++

`// C++ implementation of the approach ` `#include <bits/stdc++.h> ` `using` `namespace` `std; ` ` ` `// Function to return the count of n-bit ` `// numbers whose 0th and nth bits are set ` `int` `countNum(` `int` `n) ` `{ ` ` ` `if` `(n == 1) ` ` ` `return` `1; ` ` ` `int` `count = ` `pow` `(2, n - 2); ` ` ` `return` `count; ` `} ` ` ` `// Driver code ` `int` `main() ` `{ ` ` ` `int` `n = 3; ` ` ` `cout << countNum(n); ` ` ` ` ` `return` `0; ` `} ` |

*chevron_right*

*filter_none*

## Java

`// Java implementation of the approach ` `import` `java.io.*; ` ` ` `class` `GFG ` `{ ` ` ` `// Function to return the count of n-bit ` ` ` `// numbers whose 0th and nth bits are set ` ` ` `static` `int` `countNum(` `int` `n) ` ` ` `{ ` ` ` `if` `(n == ` `1` `) ` ` ` `return` `1` `; ` ` ` ` ` `int` `count = (` `int` `) Math.pow(` `2` `, n - ` `2` `); ` ` ` `return` `count; ` ` ` `} ` ` ` ` ` `// Driver code ` ` ` `public` `static` `void` `main (String[] args) ` ` ` `{ ` ` ` `int` `n = ` `3` `; ` ` ` `System.out.println(countNum(n)); ` ` ` `} ` `} ` ` ` `// This code is contributed by ajit ` |

*chevron_right*

*filter_none*

## Python

`# Python3 implementation of the approach ` ` ` `# Function to return the count of n-bit ` `# numbers whose 0th and nth bits are set ` `def` `countNum(n): ` ` ` `if` `(n ` `=` `=` `1` `): ` ` ` `return` `1` ` ` `count ` `=` `pow` `(` `2` `, n ` `-` `2` `) ` ` ` `return` `count ` ` ` `# Driver code ` ` ` `n ` `=` `3` `print` `(countNum(n)) ` ` ` `# This code is contributed by mohit kumar 29 ` |

*chevron_right*

*filter_none*

## C#

`// C# implementation of the approach ` `using` `System; ` ` ` `class` `GFG ` `{ ` ` ` ` ` `// Function to return the count of n-bit ` ` ` `// numbers whose 0th and nth bits are set ` ` ` `static` `int` `countNum(` `int` `n) ` ` ` `{ ` ` ` `if` `(n == 1) ` ` ` `return` `1; ` ` ` ` ` `int` `count = (` `int` `) Math.Pow(2, n - 2); ` ` ` `return` `count; ` ` ` `} ` ` ` ` ` `// Driver code ` ` ` `static` `public` `void` `Main () ` ` ` `{ ` ` ` `int` `n = 3; ` ` ` `Console.WriteLine(countNum(n)); ` ` ` `} ` `} ` ` ` `// This code is contributed by AnkitRai01 ` |

*chevron_right*

*filter_none*

**Output:**

2

Attention reader! Don’t stop learning now. Get hold of all the important CS Theory concepts for SDE interviews with the CS Theory Course at a student-friendly price and become industry ready.

## Recommended Posts:

- Program to find the Nth natural number with exactly two bits set | Set 2
- Count of elements in an Array whose set bits are in a multiple of K
- Count pairs whose sum consists of set bits only
- Count total set bits in all numbers from 1 to n | Set 2
- Count set bits in the Kth number after segregating even and odd from N natural numbers
- Find the node whose sum with X has maximum set bits
- Nth angle of a Polygon whose initial angle and per angle increment is given
- Count total set bits in all numbers from range L to R
- Count numbers in range [L, R] having K consecutive set bits
- Count numbers from range whose prime factors are only 2 and 3 using Arrays | Set 2
- Find Nth positive number whose digital root is X
- Nth number whose sum of digit is multiple of 10
- Nth positive number whose absolute difference of adjacent digits is at most 1
- Count numbers < = N whose difference with the count of primes upto them is > = K
- Count numbers in a given range whose count of prime factors is a Prime Number
- Count pairs (A, B) such that A has X and B has Y number of set bits and A+B = C
- Find a number X such that (X XOR A) is minimum and the count of set bits in X and B are equal
- Count of N digit Numbers whose sum of every K consecutive digits is equal | Set 2
- Count total unset bits in all the numbers from 1 to N
- Print all numbers whose set of prime factors is a subset of the set of the prime factors of X

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.