Reverse bits of a given 32 bits unsigned integer.
Note:
-3
and the output represents the signed integer -1073741825
.Example 1:
Input: n = 00000010100101000001111010011100 Output: 964176192 (00111001011110000010100101000000) Explanation: The input binary string 00000010100101000001111010011100 represents the unsigned integer 43261596, so return 964176192 which its binary representation is 00111001011110000010100101000000.
Example 2:
Input: n = 11111111111111111111111111111101 Output: 3221225471 (10111111111111111111111111111111) Explanation: The input binary string 11111111111111111111111111111101 represents the unsigned integer 4294967293, so return 3221225471 which its binary representation is 10111111111111111111111111111111.
Constraints:
32
Follow up: If this function is called many times, how would you optimize it?
program main
implicit none
integer(kind=4), parameter :: n = 0_4
integer(kind=4), parameter :: expected = 964176192_4
integer(kind=4), parameter :: result = reverseBits(n)
if (result /= expected) then
write (*,*) 'Test 1 failed!'
stop 1
end if
integer(kind=4), parameter :: n2 = 11111111111111111111111111111101_4
integer(kind=4), parameter :: expected2 = 3221225471_4
integer(kind=4), parameter :: result2 = reverseBits(n2)
if (result2 /= expected2) then
write (*,*) 'Test 2 failed!'
stop 1
end if
contains
pure function reverseBits(n) result(r)
implicit none
integer(kind=4), intent(in) :: n
integer(kind=4) :: r
r = iand(n, 1_4) * 8_4 &
+ iand(n, 2_4) * 4_4 &
+ iand(n, 4_4) * 2_4 &
+ iand(n, 8_4) * 1_4 &
+ iand(n, 16_4) * 8_4 &
+ iand(n, 32_4) * 4_4 &
+ iand(n, 64_4) * 2_4 &
+ iand(n, 128_4) * 1_4 &
+ iand(n, 256_4) * 8_4 &
+ iand(n, 512_4) * 4_4 &
+ iand(n, 1024_4) * 2_4 &
+ iand(n, 2048_4) * 1_4 &
+ iand(n, 4096_4) * 8_4 &
+ iand(n, 8192_4) * 4_4 &
+ iand(n, 16384_4) * 2_4 &
+ iand(n, 32768_4) * 1_4 &
+ iand(n, 65536_4) * 8_4 &
+ iand(n, 131072_4) * 4_4 &
+ iand(n, 262144_4) * 2_4 &
+ iand(n, 524288_4) * 1_4 &
+ iand(n, 1048576_4) * 8_4 &
+ iand(n, 2097152_4) * 4_4 &
+ iand(n, 4194304_4) * 2_4 &
+ iand(n, 8388608_4) * 1_4 &
+ iand(n, 16777216_4) * 8_4 &
+ iand(n, 33554432_4) * 4_4 &
+ iand(n, 67108864_4) * 2_4 &
+ iand(n, 134217728_4) * 1_4 &
+ iand(n, 268435456_4) * 8_4 &
+ iand(n, 536870912_4) * 4_4 &
+ iand(n, 1073741824_4) * 2_4 &
+ iand(n, -2147483648_4) * 1_4
end function reverseBits
temp.f95:6:42: 6 | integer(kind=4), parameter :: result = reverseBits(n) | 1 Error: Function ‘reversebits’ at (1) has no IMPLICIT type temp.f95:13:73: 13 | integer(kind=4), parameter :: n2 = 11111111111111111111111111111101_4 | 1 Error: Integer too big for its kind at (1). This check can be disabled with the option ‘-fno-range-check’ temp.f95:14:58: 14 | integer(kind=4), parameter :: expected2 = 3221225471_4 | 1 Error: Integer too big for its kind at (1). This check can be disabled with the option ‘-fno-range-check’ temp.f95:15:58: 15 | integer(kind=4), parameter :: result2 = reverseBits(n2) | 1 Error: Symbol ‘n2’ at (1) has no IMPLICIT type; did you mean ‘n’? temp.f95:60:36: 60 | + iand(n, -2147483648_4) * 1_4 | 1 Error: Integer too big for its kind at (1). This check can be disabled with the option ‘-fno-range-check’ f951: Error: Unexpected end of file in ‘temp.f95’
! This program reverses the bits of a given 32-bit unsigned integer.
program bit_reverse
implicit none
integer :: n, i, j, k, l, m, n_reversed
character(len=32) :: input_string, output_string
! Read the input from the command line
call get_command_argument(1, input_string)
read(input_string,*) n
! Initialize the output string
output_string = ""
! Reverse the bits of the input integer
do i = 1, 32
k = mod(n, 2)
n = n / 2
l = mod(n, 2)
n = n / 2
m = mod(n, 2)
n = n / 2
j = mod(n, 2)
n = n / 2
i = mod(n, 2)
n = n / 2
output_string = char(ichar(output_string) + k) // char(ichar(output_string) + l) // char(ichar(output_string) + m) // char(ichar(output_string) + j) // char(ichar(output_string) + i)
end do
! Print the output
write(*,*) "The reversed bits of the input integer are: ", output_string
end program bit_reverse
temp.f95:27:17: Error: (1) temp.f95:18:12: Error: Variable ‘i’ at (1) cannot be redefined inside loop beginning at (2) temp.f95:29:132: 29 | output_string = char(ichar(output_string) + k) // char(ichar(output_string) + l) // char(ichar(output_string) + m) // char(ichar(output_string) + j) // char(ichar(output_string) + i) | 1 Error: Line truncated at (1) [-Werror=line-truncation] temp.f95:29:132: 29 | output_string = char(ichar(output_string) + k) // char(ichar(output_string) + l) // char(ichar(output_string) + m) // char(ichar(output_string) + j) // char(ichar(output_string) + i) | 1 Error: Function ‘ichar’ requires an argument list at (1) f951: some warnings being treated as errors
def reverse_bits(n: int) -> int:
res = 0
for _ in range(32):
res <<= 1
res |= n & 1
n >>= 1
return res
The algorithm iterates through all 32 bits of the given unsigned 32-bit integer, and for each bit, it shifts the result variable res
one position to the left, and then performs a bitwise OR with the given number n
ANDed with 1. This operation adds the least significant bit of n
to the most significant bit of res
. Then, the given number n
is shifted one position to the right. The process is repeated for all 32 bits, and in each step, the result variable res
accumulates the reversed bits of the input number.
unsigned int reverseBits(unsigned int n) {
unsigned int res = 0;
for (int i = 0; i < 32; ++i) {
res <<= 1;
res |= n & 1;
n >>= 1;
}
return res;
}
The algorithm iterates through all 32 bits of the given unsigned 32-bit integer, and for each bit, it shifts the result variable res
one position to the left, and then performs a bitwise OR with the given number n
ANDed with 1. This operation adds the least significant bit of n
to the most significant bit of res
. Then, the given number n
is shifted one position to the right. The process is repeated for all 32 bits, and in each step, the result variable res
accumulates the reversed bits of the input number.