Skip to content

[Hacker Rank] Interview Preparation Kit: Miscellaneous: Flipping bits… #223

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jul 5, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,120 @@
# [Miscellaneous: Flipping bits](https://www.hackerrank.com/challenges/flipping-bits)

- Difficulty: `#easy`
- Category: `#ProblemSolvingBasic` `#BitManipulation`

You will be given a list of 32 bit unsigned integers.
Flip all the bits ( `1 -> 0` and `0 -> 1`)
and return the result as an unsigned integer.

## Example

$ n = 9_{10} $. We're working with 32 bits, so:

$ 9_{10} = 1001_{2} $

$ 00000000000000000000000000001001_{2} = 9_{10} $
$ 11111111111111111111111111110110_{2} = 4294967286_{10} $

Return `4294967286`

## Function Description

Complete the flippingBits function in the editor below.

flippingBits has the following parameter(s):

- `int n`: an integer

## Returns

- `int`: the unsigned decimal integer result

## Input Format

The first line of the input contains `q`, the number of queries.
Each of the next `q` lines contain an integer, `n`, to process.

## Constraints

- $ 1 \leq q \leq 100 $
- $ 0 \leq n \leq 2^{32} $

## Sample Input 0

```text
3
2147483647
1
0
```

## Sample Output 0

```text
2147483648
4294967294
4294967295
```

## Explanation 0

$ 01111111111111111111111111111111_{2} = 2147483647_{10} $
$ 10000000000000000000000000000000_{2} = 2147483648_{10} $

$ 00000000000000000000000000000001_{2} = 1_{10} $
$ 11111111111111111111111111111110_{2} = 4294967294_{10} $

$ 00000000000000000000000000000000_{2} = 0_{10} $
$ 11111111111111111111111111111110_{2} = 4294967295_{10} $

## Sample Input 1

```text
2
4
123456
```

## Sample Output 1

```text
4294967291
4294843839
```

## Explanation 1

$ 00000000000000000000000000000100_{2} = 4_{10} $
$ 11111111111111111111111111111011_{2} = 4294967291_{10} $

$ 00000000000000011110001001000000_{2} = 4_{10} $
$ 11111111111111100001110110111111_{2} = 429484389_{10} $

## Sample Input 2

```text
3
0
802743475
35601423
```

## Sample Output 2

```text
4294967295
3492223820
4259365872
```

## Explanation 2

$ 00000000000000000000000000000000_{2} = 4_{10} $
$ 11111111111111111111111111111111_{2} = 4294967295_{10} $

$ 00101111110110001110010010110011_{2} = 802743475_{10} $
$ 11010000001001110001101101001100_{2} = 3492223820_{10} $

$ 00000010000111110011110000001111_{2} = 35601423_{10} $
$ 11111101111000001100001111110000_{2} = 4259365872_{10} $
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
// @link Problem definition [[docs/hackerrank/interview_preparation_kit/miscellaneous/flipping-bits.md]]

namespace algorithm_exercises_csharp.hackerrank.interview_preparation_kit.miscellaneous;

using System.Diagnostics.CodeAnalysis;
using System.Text;

public static class FlippingBits
{
public static long flippingBits(long n)
{
string n_bin_str = Convert.ToString(n, 2);
n_bin_str = n_bin_str.PadLeft(32, '0'); // Ensure 32 bits
StringBuilder result_bin_str = new StringBuilder();

foreach (char bin_digit in n_bin_str)
{
if (bin_digit == '1')
{
result_bin_str.Append('0');
}
else
{
result_bin_str.Append('1');
}
}

long number = Convert.ToUInt32(result_bin_str.ToString(), 2);

return number;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
[
{
"title": "Sample Test Case 0",
"tests": [
{
"input": 2147483647,
"answer": 2147483648
},
{
"input": 1,
"answer": 4294967294
},
{
"input": 0,
"answer": 4294967295
}
]
},
{
"title": "Sample Test Case 1",
"tests": [
{
"input": 4,
"answer": 4294967291
},
{
"input": 123456,
"answer": 4294843839
}
]
},
{
"title": "Sample Test Case 2",
"tests": [
{
"input": 0,
"answer": 4294967295
},
{
"input": 802743475,
"answer": 3492223820
},
{
"input": 35601423,
"answer": 4259365872
}
]
}
]
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
namespace algorithm_exercises_csharp_test.hackerrank.interview_preparation_kit.miscellaneous;

using algorithm_exercises_csharp_test.common;
using algorithm_exercises_csharp.hackerrank.interview_preparation_kit.miscellaneous;

[TestClass]
public class FlippingBitsTest
{
public class FlippingBitsTestCaseTest(long input, long answer)
{
public long Input { get; } = input;
public long Answer { get; } = answer;
}

public class FlippingBitsTestCase(string title, List<FlippingBitsTestCaseTest> tests)
{
public string Title { get; } = title;
public List<FlippingBitsTestCaseTest> Tests { get; } = tests;
}

private List<FlippingBitsTestCase> testCases { get; set; } = default!;

[TestInitialize]
public void testInitialize()
{
testCases = JsonLoader.resourceLoad<List<FlippingBitsTestCase>>(
"hackerrank/interview_preparation_kit/miscellaneous/flipping_bits.testcases.json"
) ?? [];
}

[TestMethod]
public void testFlippingBits()
{
long result;

foreach (FlippingBitsTestCase tests in testCases)
{
foreach (FlippingBitsTestCaseTest test in tests.Tests)
{
result = FlippingBits.flippingBits(test.Input);
Assert.AreEqual(
test.Answer,
result,
string.Format(
System.Globalization.CultureInfo.InvariantCulture,
"FlippingBits.flippingBits({0}) => must be: {1}",
test.Input,
test.Answer
)
);
}
}
}
}