This README explains how to find the complement of a given number across various programming languages. Each step in the code is broken down to help you understand the underlying logic.
-
Initialize the Mask:
- A variable
mask
is initialized to 0. This will eventually represent a bitmask where all bits are set to 1, corresponding to the binary length of the input numbernum
.
- A variable
-
Copy the Input Number:
- Create a temporary variable
temp
initialized with the value ofnum
. This is used to determine the binary length ofnum
.
- Create a temporary variable
-
Generate the Bitmask:
- The loop runs while
temp
is not 0. In each iteration:- Shift the current bits in
mask
to the left by 1 position to make room for the new bit. - Set the least significant bit of
mask
to 1. - Right shift
temp
by 1 to move to the next bit in the binary representation.
- Shift the current bits in
- The loop runs while
-
Compute the Complement:
- Use XOR between
num
andmask
to flip all bits ofnum
, producing its complement.
- Use XOR between
-
Initialize the Mask:
- A variable
mask
is initialized to 0, which will be used to create a bitmask where all bits are set to 1.
- A variable
-
Create a Temporary Copy:
- Copy the input number
num
into a temporary variabletemp
to determine the number of bits innum
.
- Copy the input number
-
Create the Bitmask:
- Loop until
temp
becomes 0:- Left shift
mask
by 1 to make space for the new bit. - OR the mask with 1 to set the least significant bit to 1.
- Right shift
temp
by 1 to move to the next bit.
- Left shift
- Loop until
-
Find the Complement:
- XOR
num
with the mask to flip all bits and get the complement.
- XOR
-
Initialize the Mask:
- The
mask
is initialized to 0. It will eventually have all bits set to 1 within the range ofnum
.
- The
-
Create a Temporary Variable:
- Store the value of
num
in a temporary variabletemp
for manipulation.
- Store the value of
-
Create the Bitmask:
- Loop until
temp
is 0:- Shift
mask
to the left by 1 and set the least significant bit to 1. - Right shift
temp
by 1 to move to the next bit.
- Shift
- Loop until
-
Calculate the Complement:
- XOR
num
with the mask to flip all bits, effectively finding the complement.
- XOR
-
Initialize the Mask:
- A variable
mask
is initialized to 0. This mask will create a number with all bits set to 1, matching the length ofnum
's binary representation.
- A variable
-
Temporary Variable Setup:
- Initialize
temp
tonum
to help determine the binary length ofnum
.
- Initialize
-
Generate the Mask:
- While
temp
is not 0:- Left shift
mask
by 1 to make space for the next bit and set the last bit to 1. - Right shift
temp
by 1 to move to the next bit.
- Left shift
- While
-
Find the Complement:
- XOR
num
withmask
to flip all bits and compute the complement.
- XOR
-
Initialize the Mask:
- The
mask
is initialized to 0. It will be used to cover all the bits ofnum
.
- The
-
Create a Temporary Variable:
- Store
num
in a temporary variabletemp
, which will help in determining the bit length ofnum
.
- Store
-
Create the Bitmask:
- The loop continues while
temp
is not 0:- Shift the
mask
to the left by 1 bit and set the rightmost bit to 1. - Right shift
temp
by 1 bit to reduce its length.
- Shift the
- The loop continues while
-
Compute the Complement:
- XOR
num
withmask
to flip all bits and find the complement.
- XOR