Given a Binary Number as a string, print its 1’s and 2’s complements.
1’s complement of a binary number is another binary number obtained by toggling all bits in it, i.e., transforming the 0 bit to 1 and the 1 bit to 0.
1's complement of "0111" is "1000" 1's complement of "1100" is "0011"
2’s complement of a binary number is 1 added to the 1’s complement of the binary number.
2's complement of "0111" is "1001" 2's complement of "1100" is "0100"
For one’s complement, we simply need to flip all bits.
For 2’s complement, we first find one’s complement. We traverse the one’s complement starting from LSB (least significant bit), and look for 0. We flip all 1’s (change to 0) until we find a 0. Finally, we flip the found 0. For example, 2’s complement of “01000” is “11000” (Note that we first find one’s complement of 01000 as 10111). If there are all 1’s (in one’s complement), we add an extra 1 in the string. For example, 2’s complement of “000” is “1000” (1’s complement of “000” is “111”).
Below is the implementation.
1's complement: 0011 2's complement: 0100
Thanks to Utkarsh Trivedi for above solution.
As a side note, signed numbers generally use 2’s complement representation. Positive values are stored as it is and negative values are stored in their 2’s complement form. One extra bit is required to indicate whether number is positive or negative. For example char is 8 bits in C. If 2’s complement representation is used for char, then 127 is stored as it is, i.e., 01111111 where first 0 indicates positive. But -127 is stored as 10000001.
Related Post :
Efficient method for 2’s complement of a binary string
Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.
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.
- Check if binary representation of a given number and its complement are anagram
- Efficient method for 2's complement of a binary string
- 9's complement of a decimal number
- Previous number same as 1's complement
- Complement of a number with any base b
- 10's Complement of a decimal number
- 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
- What’s difference between 1's Complement and 2's Complement?
- Subtraction of two numbers using 2's Complement
- Check if one of the numbers is one's complement of the other
- Find One's Complement of an Integer
- Why are negative numbers stored as 2's complement?
- Check if bits in range L to R of two numbers are complement of each other or not
- Find relative complement of two sorted arrays
- 8085 program to find 2's complement of the contents of Flag Register
- Count number of trailing zeros in Binary representation of a number using Bitset
- Number of ways to split a binary number such that every part is divisible by 2
- Check if the binary representation of a number has equal number of 0s and 1s in blocks