The complement of an integer is the integer you get when you flip all the 0's to 1's and all the 1's to 0's in its binary representation.
5 is "101 " in binary and its complement is "010 " which is the integer 2.Given an integer num, return its complement.
Example 1:
Input: num = 5 Output: 2 Explanation: The binary representation of 5 is 101 (no leading zero bits), and its complement is 010. So you need to output 2.
Example 2:
Input: num = 1 Output: 0 Explanation: The binary representation of 1 is 1 (no leading zero bits), and its complement is 0. So you need to output 0.
Constraints:
1 <= num < 231Note: This question is the same as 1009: https://leetcode.com/problems/complement-of-base-10-integer/
program main
implicit none
integer :: num, complement
! Example 1
num = 5
write (*, '(A, I0)') 'Complement of ', num, ' is ', complement(num)
! Example 2
num = 1
write (*, '(A, I0)') 'Complement of ', num, ' is ', complement(num)
contains
function complement(num) result(res)
implicit none
integer, intent(in) :: num
integer :: res
res = btest(num, 0_8)
end function complement
end program main
temp.f95:16:24:
16 | function complement(num) result(res)
| 1
Error: Procedure ‘complement’ at (1) has an explicit interface from a previous declaration
temp.f95:17:21:
17 | implicit none
| 1
Error: Unexpected IMPLICIT NONE statement in CONTAINS section at (1)
temp.f95:18:34:
18 | integer, intent(in) :: num
| 1
Error: Unexpected data declaration statement in CONTAINS section at (1)
temp.f95:19:22:
19 | integer :: res
| 1
Error: Unexpected data declaration statement in CONTAINS section at (1)
temp.f95:21:29:
21 | res = btest(num, 0_8)
| 1
Error: Unexpected assignment statement in CONTAINS section at (1)
temp.f95:22:7:
22 | end function complement
| 1
Error: Expecting END PROGRAM statement at (1)
!include(header.f90)
program complement
implicit none
integer :: num, complement
! Test case 1:
num = 5
complement = get_complement(num)
write (*,*) "Complement of ", num, " is ", complement
! Test case 2:
num = 1
complement = get_complement(num)
write (*,*) "Complement of ", num, " is ", complement
contains
function get_complement(num) result(complement)
implicit none
integer, intent(in) :: num
integer :: complement
! Convert the integer to binary
integer :: binary_num(32)
binary_num = 0
do i = 1, 32
binary_num(i) = mod(num, 2)
num = num / 2
end do
! Flip all the 0's to 1's and all the 1's to 0's
do i = 1, 32
if (binary_num(i) == 0) then
binary_num(i) = 1
else
binary_num(i) = 0
end if
end do
! Convert the binary array back to an integer
complement = 0
do i = 1, 32
complement = complement * 2 + binary_num(i)
end do
end function get_complement
end program complement
!include(footer.f90)
temp.f95:6:30:
6 | integer :: num, complement
| 1
Error: Symbol ‘complement’ at (1) cannot have a type
temp.f95:10:15:
10 | complement = get_complement(num)
| 1
Error: ‘complement’ at (1) is not a variable
temp.f95:11:57:
11 | write (*,*) "Complement of ", num, " is ", complement
| 1
Error: Symbol at (1) is not appropriate for an expression
temp.f95:15:15:
15 | complement = get_complement(num)
| 1
Error: ‘complement’ at (1) is not a variable
temp.f95:16:57:
16 | write (*,*) "Complement of ", num, " is ", complement
| 1
Error: Symbol at (1) is not appropriate for an expression
temp.f95:9:7:
9 | num = 5
| 1
Error: Symbol ‘num’ at (1) has no IMPLICIT type
temp.f95:28:12:
28 | do i = 1, 32
| 1
Error: Symbol ‘i’ at (1) has no IMPLICIT type
temp.f95:30:12:
30 | num = num / 2
| 1
Error: Dummy argument ‘num’ with INTENT(IN) in variable definition context (assignment) at (1)
def find_complement(num: int) -> int:
bit_length = 0
mask = num
while mask > 0:
mask >>= 1
bit_length += 1
all_ones = (1 << bit_length) - 1
return num ^ all_ones
First, we calculate the number of bits in the binary representation of the input integer num. We do this by consecutively right-shifting the number (bitwise operation) and counting the iterations until the number becomes 0. This gives us the length of the binary representation.
Once we have the length, we create a bitmask of the same length with all bits set to 1 (for example, if the length is 3, the bitmask would be 111 in binary). To get the complement of the input integer, we then XOR (^ operator) the number with this all-1 mask. The XOR operation returns 1 when the input bits are different and 0 when they are the same, so if we have a 0 in the input, we will get 1 and vice versa. This way, we get the complement of the original number.
Finally, we return the resulting number, which is the complement of the input integer num.
int findComplement(int num) {
int bitLength = 0;
int mask = num;
while (mask > 0) {
mask >>= 1;
bitLength++;
}
int allOnes = (1 << bitLength) - 1;
return num ^ allOnes;
}
First, we calculate the number of bits in the binary representation of the input integer num. We do this by consecutively right-shifting the number (bitwise operation) and counting the iterations until the number becomes 0. This gives us the length of the binary representation.
Once we have the length, we create a bitmask of the same length with all bits set to 1 (for example, if the length is 3, the bitmask would be 111 in binary). To get the complement of the input integer, we then XOR (^ operator) the number with this all-1 mask. The XOR operation returns 1 when the input bits are different and 0 when they are the same, so if we have a 0 in the input, we will get 1 and vice versa. This way, we get the complement of the original number.
Finally, we return the resulting number, which is the complement of the input integer num.