# Fibbinary Numbers (No consecutive 1s in binary)

Given N, check if the number is Fibbinary Number or not. Fibbinary numbers are integers whose binary representation contains no consecutive ones.

Examples :

Input : 10 Output : YES Explanation: 1010 is the binary representation of 10 which does not contains any consecutive 1's. Input : 11 Output : NO Explanation: 1011 is the binary representation of 11, which contains consecutive 1's

The idea to do this is to right shift the number, till n!=0. For every binary representation of 1, check if the last bit found was 1 or not. Get the last bit of binary representation of the integer by doing a (n&1). If the last bit of the binary representation is 1 and the previous bit before doing a right shift was also one, we encounter consecutive 1’s. So we come to the conclusion that it is not a fibonnary number.

Some of the first few Fibonnary numbers are:

0, 2, 4, 8, 10, 16, 18, 20.......

## CPP

`// CPP program to check if a number ` `// is fibinnary number or not ` `#include <iostream> ` `using` `namespace` `std; ` ` ` `// function to check if binary ` `// representation of an integer ` `// has consecutive 1s ` `bool` `checkFibinnary(` `int` `n) ` `{ ` ` ` `// stores the previous last bit ` ` ` `// initially as 0 ` ` ` `int` `prev_last = 0; ` ` ` ` ` `while` `(n) ` ` ` `{ ` ` ` `// if current last bit and ` ` ` `// previous last bit is 1 ` ` ` `if` `((n & 1) && prev_last) ` ` ` `return` `false` `; ` ` ` ` ` `// stores the last bit ` ` ` `prev_last = n & 1; ` ` ` ` ` `// right shift the number ` ` ` `n >>= 1; ` ` ` `} ` ` ` ` ` `return` `true` `; ` `} ` ` ` `// Driver code to check above function ` `int` `main() ` `{ ` ` ` `int` `n = 10; ` ` ` `if` `(checkFibinnary(n)) ` ` ` `cout << ` `"YES"` `; ` ` ` `else` ` ` `cout << ` `"NO"` `; ` ` ` `return` `0; ` `} ` |

*chevron_right*

*filter_none*

## Java

`// Java program to check if a number ` `// is fibinnary number or not ` `class` `GFG { ` ` ` ` ` `// function to check if binary ` ` ` `// representation of an integer ` ` ` `// has consecutive 1s ` ` ` `static` `boolean` `checkFibinnary(` `int` `n) ` ` ` `{ ` ` ` ` ` `// stores the previous last bit ` ` ` `// initially as 0 ` ` ` `int` `prev_last = ` `0` `; ` ` ` ` ` `while` `(n != ` `0` `) ` ` ` `{ ` ` ` ` ` `// if current last bit and ` ` ` `// previous last bit is 1 ` ` ` `if` `((n & ` `1` `) != ` `0` `&& prev_last != ` `0` `) ` ` ` ` ` `return` `false` `; ` ` ` ` ` `// stores the last bit ` ` ` `prev_last = n & ` `1` `; ` ` ` ` ` `// right shift the number ` ` ` `n >>= ` `1` `; ` ` ` `} ` ` ` ` ` `return` `true` `; ` ` ` `} ` ` ` ` ` `// Driver code to check above function ` ` ` `public` `static` `void` `main(String[] args) ` ` ` `{ ` ` ` `int` `n = ` `10` `; ` ` ` ` ` `if` `(checkFibinnary(n) == ` `true` `) ` ` ` `System.out.println(` `"YES"` `); ` ` ` `else` ` ` `System.out.println(` `"NO"` `); ` ` ` `} ` `} ` ` ` `// This code is contributed by ` `// Smitha Dinesh Semwal ` |

*chevron_right*

*filter_none*

## Python3

`# Python 3 program to check if a ` `# number is fibinnary number or ` `# not ` ` ` `# function to check if binary ` `# representation of an integer ` `# has consecutive 1s ` `def` `checkFibinnary(n): ` ` ` ` ` `# stores the previous last bit ` ` ` `# initially as 0 ` ` ` `prev_last ` `=` `0` ` ` ` ` `while` `(n): ` ` ` ` ` `# if current last bit and ` ` ` `# previous last bit is 1 ` ` ` `if` `((n & ` `1` `) ` `and` `prev_last): ` ` ` `return` `False` ` ` ` ` `# stores the last bit ` ` ` `prev_last ` `=` `n & ` `1` ` ` ` ` `# right shift the number ` ` ` `n >>` `=` `1` ` ` ` ` ` ` `return` `True` ` ` `# Driver code ` `n ` `=` `10` ` ` `if` `(checkFibinnary(n)): ` ` ` `print` `(` `"YES"` `) ` `else` `: ` ` ` `print` `(` `"NO"` `) ` ` ` `# This code is contributed by Smitha Dinesh Semwal ` |

*chevron_right*

*filter_none*

## C#

`// C# program to check if a number ` `// is fibinnary number or not ` `using` `System; ` ` ` `class` `GFG { ` ` ` ` ` `// function to check if binary ` ` ` `// representation of an integer ` ` ` `// has consecutive 1s ` ` ` `static` `bool` `checkFibinnary(` `int` `n) ` ` ` `{ ` ` ` ` ` `// stores the previous last bit ` ` ` `// initially as 0 ` ` ` `int` `prev_last = 0; ` ` ` ` ` `while` `(n != 0) ` ` ` `{ ` ` ` ` ` `// if current last bit and ` ` ` `// previous last bit is 1 ` ` ` `if` `((n & 1) != 0 && prev_last != 0) ` ` ` ` ` `return` `false` `; ` ` ` ` ` `// stores the last bit ` ` ` `prev_last = n & 1; ` ` ` ` ` `// right shift the number ` ` ` `n >>= 1; ` ` ` `} ` ` ` ` ` `return` `true` `; ` ` ` `} ` ` ` ` ` `// Driver code to check above function ` ` ` `public` `static` `void` `Main() ` ` ` `{ ` ` ` `int` `n = 10; ` ` ` ` ` `if` `(checkFibinnary(n) == ` `true` `) ` ` ` `Console.WriteLine(` `"YES"` `); ` ` ` `else` ` ` `Console.WriteLine(` `"NO"` `); ` ` ` `} ` `} ` ` ` `// This code is contributed by vt_m. ` |

*chevron_right*

*filter_none*

## PHP

`<?php ` `// PHP program to check if a number ` `// is fibinnary number or not ` ` ` `// function to check if binary ` `// representation of an integer ` `// has consecutive 1s ` `function` `checkFibinnary(` `$n` `) ` `{ ` ` ` `// stores the previous last bit ` ` ` `// initially as 0 ` ` ` `$prev_last` `= 0; ` ` ` ` ` `while` `(` `$n` `) ` ` ` `{ ` ` ` `// if current last bit and ` ` ` `// previous last bit is 1 ` ` ` `if` `((` `$n` `& 1) && ` `$prev_last` `) ` ` ` `return` `false; ` ` ` ` ` `// stores the last bit ` ` ` `$prev_last` `= ` `$n` `& 1; ` ` ` ` ` `// right shift the number ` ` ` `$n` `>>= 1; ` ` ` `} ` ` ` `return` `true; ` `} ` ` ` `// Driver code ` `$n` `= 10; ` `if` `(checkFibinnary(` `$n` `)) ` ` ` `echo` `"YES"` `; ` `else` ` ` `echo` `"NO"` `; ` ` ` `// This code is contributed by mits ` `?> ` |

*chevron_right*

*filter_none*

**Output :**

YES

**Time Complexity:** O( log(n) )

Fibbinary Numbers (No consecutive 1s in binary) – O(1) Approach

## Recommended Posts:

- Fibbinary Numbers (No consecutive 1s in binary) - O(1) Approach
- 1 to n bit numbers with no consecutive 1s in binary representation.
- 1 to n bit numbers with no consecutive 1s in binary representation
- Print numbers such that no two consecutive numbers are co-prime and every three consecutive numbers are co-prime
- Permutation of numbers such that sum of two consecutive numbers is a perfect square
- Length of the Longest Consecutive 1s in Binary Representation
- Find consecutive 1s of length >= n in binary representation of a number
- Expressing factorial n as sum of consecutive numbers
- Express a number as sum of consecutive numbers
- Length of longest consecutive zeroes in the binary representation of a number.
- Find the number of consecutive zero at the end after multiplying n numbers
- Check if a number can be expressed as a sum of consecutive numbers
- Prove that atleast one of three consecutive even numbers is divisible by 6
- Find the prime numbers which can written as sum of most consecutive primes
- Count ways to express a number as sum of consecutive numbers

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.