# Previous number same as 1’s complement

Given a number check whether binary representation of its predecessor and its 1’s complement are same or not.

**Examples:**

Input : 14

Output : NO

Storing 14 as a 4 bit number, 14 (1110), its predecessor 13 (1101), its 1’s complement 1 (0001), 13 and 1 are not same in their binary representation and hence output is NO.Input : 8

Output : YES

Storing 8 as a 4 bit number, 8 (1000), its predecessor 7 (0111), its 1’s complement 7 (0111), both its predecessor and its 1’s complement are 7 and hence output is YES.

**Simple Approach:** In this approach, we actually calculate the complement of the number.

1. Find binary representation of the number’s predecessor and it’s 1’s complement using simple decimal to binary representation technique.

2. Compare bit by bit to check whether they are equal or not.

3. If all bits are equal then print YES else print NO.

**Time Complexity:** O (log n), as binary representation of numbers is getting calculated.

**Auxiliary Space:** O (1), although auxiliary space is O (1) still some memory spaces are getting

used to store binary representation of the numbers.

**Efficient Approach:** Only numbers which are powers of 2 have binary representation of their predecessor and their 1’s complement as same.

1. Check whether a number is power of 2 or not.

2. If a number is power of 2 then print YES else print NO.

## C++

`// An efficient C++ program to check if binary ` `// representations of n's predecessor and its ` `// 1's complement are same. ` `#include <bits/stdc++.h> ` `#define ull unsigned long long int ` `using` `namespace` `std; ` ` ` `// Returns true if binary representations of ` `// n's predecessor and it's 1's complement are same. ` `bool` `bit_check(ull n) ` `{ ` ` ` `if` `((n & (n - 1)) == 0) ` ` ` `return` `true` `; ` ` ` `return` `false` `; ` `} ` ` ` `int` `main() ` `{ ` ` ` `ull n = 14; ` ` ` `cout << bit_check(n) << endl; ` ` ` `return` `0; ` `} ` |

*chevron_right*

*filter_none*

## Java

`// An efficient java program to check if binary ` `// representations of n's predecessor and its ` `// 1's complement are same. ` `public` `class` `GFG { ` ` ` ` ` `// Returns true if binary representations of ` ` ` `// n's predecessor and it's 1's complement ` ` ` `// are same. ` ` ` `static` `boolean` `bit_check(` `int` `n) ` ` ` `{ ` ` ` `if` `((n & (n - ` `1` `)) == ` `0` `) ` ` ` `return` `true` `; ` ` ` `return` `false` `; ` ` ` `} ` ` ` ` ` `// Driver code ` ` ` `public` `static` `void` `main(String args[]) { ` ` ` ` ` `int` `n = ` `14` `; ` ` ` `if` `(bit_check(n)) ` ` ` `System.out.println (` `'1'` `); ` ` ` `else` ` ` `System.out.println(` `'0'` `); ` ` ` ` ` `} ` `} ` ` ` `// This code is contributed by Sam007 ` |

*chevron_right*

*filter_none*

## Python3

# An efficient Python 3 program to check

# if binary representations of n’s predecessor

# and its 1’s complement are same.

# Returns true if binary representations

# of n’s predecessor and it’s 1’s

# complement are same.

def bit_check(n):

if ((n & (n – 1)) == 0):

return True

return False

# Driver Code

if __name__ == ‘__main__’:

n = 14

if(bit_check(n)):

print(‘1’)

else:

print(‘0’)

# This code is contributed by

# Surendra_Gangwar

## C#

`// An efficient C# program to check if binary ` `// representations of n's predecessor and its ` `// 1's complement are same. ` `using` `System; ` `using` `System.Collections.Generic; ` ` ` `class` `GFG { ` ` ` ` ` `// Returns true if binary representations of ` ` ` `// n's predecessor and it's 1's complement ` ` ` `// are same. ` ` ` `static` `bool` `bit_check(` `int` `n) ` ` ` `{ ` ` ` `if` `((n & (n - 1)) == 0) ` ` ` `return` `true` `; ` ` ` `return` `false` `; ` ` ` `} ` ` ` ` ` `public` `static` `void` `Main() ` ` ` `{ ` ` ` `int` `n = 14; ` ` ` `if` `(bit_check(n)) ` ` ` `Console.WriteLine (` `'1'` `); ` ` ` `else` ` ` `Console.WriteLine (` `'0'` `); ` ` ` `} ` `} ` ` ` `// This code is contributed by Sam007 ` |

*chevron_right*

*filter_none*

## PHP

`<?php ` `// An efficient PHP program to check ` `// if binary representations of n's ` `// predecessor and its 1's complement ` `// are same. ` ` ` `// Returns true if binary ` `// representations of n's ` `// predecessor and it's 1's ` `// complement are same. ` `function` `bit_check(` `$n` `) ` `{ ` ` ` `if` `((` `$n` `& (` `$n` `- 1)) == 0) ` ` ` `return` `1; ` ` ` `return` `0; ` `} ` ` ` ` ` `// Driver code ` ` ` `$n` `= 14; ` ` ` `echo` `bit_check(` `$n` `); ` ` ` `// This code is contributed by Sam007. ` `?> ` |

*chevron_right*

*filter_none*

**Output:**

0

**Time Complexity:** O (1)

**Auxiliary Space :** O (1) No extra space is getting used.

## Recommended Posts:

- 9's complement of a decimal number
- 10's Complement of a decimal number
- 1's and 2's complement of a Binary Number
- Complement of a number with any base b
- 8085 program to find 1’s and 2’s complement of 16-bit number
- 8085 program to find 1's and 2's complement of 8-bit number
- Interface 8255 with 8085 microprocessor for 1’s and 2’s complement of a number
- Check if binary representation of a given number and its complement are anagram
- Binary representation of previous number
- Previous smaller integer having one less number of set bits
- What’s difference between 1's Complement and 2's Complement?
- Subtraction of two numbers using 2's Complement
- Find One's Complement of an Integer
- Check if one of the numbers is one's complement of the other
- Why are negative numbers stored as 2's complement?

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.