# Position of rightmost bit with first carry in sum of two binary

Given two non-negative integers **a** and **b**. The problem is to find the position of the rightmost bit where a carry is generated in the binary addition of **a** and **b**.

Examples:

Input : a = 10, b = 2 Output : 2(10)= (1010)_{10}_{2}(2)= (10)_{10}_{2}. 1010 +10 As highlighted, 1st carry bit from the right will be generated at position '2'. Input : a = 10, b = 5 Output : 0 '0' as no carry bit will be generated.

**Approach:** Following are the steps:

- Calculate
**num**= a & b. - Find the position of rightmost set bit in
**num**.

## CPP

`// C++ implementation to find the position of ` `// rightmost bit where a carry is generated first ` `#include <bits/stdc++.h> ` `using` `namespace` `std; ` ` ` `typedef` `unsigned ` `long` `long` `int` `ull; ` ` ` `// function to find the position of ` `// rightmost set bit in 'n' ` `unsigned ` `int` `posOfRightmostSetBit(ull n) ` `{ ` ` ` `return` `log2(n & -n) + 1; ` `} ` ` ` `// function to find the position of rightmost ` `// bit where a carry is generated first ` `unsigned ` `int` `posOfCarryBit(ull a, ull b) ` `{ ` ` ` `return` `posOfRightmostSetBit(a & b); ` `} ` ` ` `// Driver program to test above ` `int` `main() ` `{ ` ` ` `ull a = 10, b = 2; ` ` ` `cout << posOfCarryBit(a, b); ` ` ` `return` `0; ` `} ` |

*chevron_right*

*filter_none*

## Java

`// Java implementation to find the position of ` `// rightmost bit where a carry is generated first ` `class` `GFG { ` ` ` ` ` `// function to find the position of ` ` ` `// rightmost set bit in 'n' ` ` ` `static` `int` `posOfRightmostSetBit(` `int` `n) ` ` ` `{ ` ` ` `return` `(` `int` `)(Math.log(n & -n) / Math.log(` `2` `)) + ` `1` `; ` ` ` `} ` ` ` ` ` `// function to find the position of rightmost ` ` ` `// bit where a carry is generated first ` ` ` `static` `int` `posOfCarryBit(` `int` `a, ` `int` `b) ` ` ` `{ ` ` ` `return` `posOfRightmostSetBit(a & b); ` ` ` `} ` ` ` ` ` `// Driver code ` ` ` `public` `static` `void` `main(String[] args) ` ` ` `{ ` ` ` `int` `a = ` `10` `, b = ` `2` `; ` ` ` ` ` `System.out.print(posOfCarryBit(a, b)); ` ` ` `} ` `} ` ` ` `// This code is contributed by Anant Agarwal. ` |

*chevron_right*

*filter_none*

## Python3

`# Python3 implementation to find the position of ` `# rightmost bit where a carry is generated first ` ` ` `import` `math ` ` ` `# function to find the position of ` `# rightmost set bit in 'n' ` `def` `posOfRightmostSetBit( n ): ` ` ` `return` `int` `(math.log2(n & ` `-` `n) ` `+` `1` `) ` ` ` `# function to find the position of rightmost ` `# bit where a carry is generated first ` `def` `posOfCarryBit( a , b ): ` ` ` `return` `posOfRightmostSetBit(a & b) ` ` ` `# Driver program to test above ` `a ` `=` `10` `b ` `=` `2` `print` `(posOfCarryBit(a, b)) ` ` ` `# This code is contributed by "Sharad_Bhardwaj". ` |

*chevron_right*

*filter_none*

## C#

`// C# implementation to find the position of ` `// rightmost bit where a carry is generated first ` `using` `System; ` ` ` `class` `GFG { ` ` ` ` ` `// function to find the position of ` ` ` `// rightmost set bit in 'n' ` ` ` `static` `int` `posOfRightmostSetBit(` `int` `n) ` ` ` `{ ` ` ` `return` `(` `int` `)(Math.Log(n & -n) / Math.Log(2)) + 1; ` ` ` `} ` ` ` ` ` `// function to find the position of rightmost ` ` ` `// bit where a carry is generated first ` ` ` `static` `int` `posOfCarryBit(` `int` `a, ` `int` `b) ` ` ` `{ ` ` ` `return` `posOfRightmostSetBit(a & b); ` ` ` `} ` ` ` ` ` `// Driver code ` ` ` `public` `static` `void` `Main() ` ` ` `{ ` ` ` `int` `a = 10, b = 2; ` ` ` ` ` `Console.Write(posOfCarryBit(a, b)); ` ` ` `} ` `} ` ` ` `// This code is contributed by Sam007. ` |

*chevron_right*

*filter_none*

Output:

2

Don’t stop now and take your learning to the next level. Learn all the important concepts of Data Structures and Algorithms with the help of the most trusted course: DSA Self Paced. Become industry ready at a student-friendly price.

## Recommended Posts:

- Position of rightmost set bit
- Turn off the rightmost set bit
- Binary representation of a given number
- Find position of the only set bit
- Check if binary representation of a number is palindrome
- 1's and 2's complement of a Binary Number
- Gray to Binary and Binary to Gray conversion
- Length of the Longest Consecutive 1s in Binary Representation
- XOR counts of 0s and 1s in binary representation
- Maximum 0's between two immediate 1's in binary representation
- Efficient method for 2's complement of a binary string
- Check divisibility in a binary stream
- Find longest sequence of 1's in binary representation with one flip
- Convert decimal fraction to binary number
- Binary string with given frequencies of sums of consecutive pairs of characters
- Check in binary array the number represented by a subarray is odd or even
- Remove one bit from a binary number to get maximum value
- Position of rightmost different bit
- Convert a binary number to octal
- Set the rightmost unset bit

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.