Skip to content

Solution implemented - #88

Open
NikolayAbysov wants to merge 3 commits into
mate-academy:masterfrom
NikolayAbysov:hw2-flip-bit
Open

Solution implemented#88
NikolayAbysov wants to merge 3 commits into
mate-academy:masterfrom
NikolayAbysov:hw2-flip-bit

Conversation

@NikolayAbysov

Copy link
Copy Markdown

No description provided.

Comment on lines +12 to +28
bitIndex = bitIndex - 1;
StringBuilder stringBuilder = new StringBuilder(Integer.toBinaryString(value));
stringBuilder = stringBuilder.reverse();

if (stringBuilder.length() > bitIndex) {
if (stringBuilder.charAt(bitIndex) == '1') {
stringBuilder.setCharAt(bitIndex, '0');
} else {
stringBuilder.setCharAt(bitIndex, '1');
}
} else {
while (stringBuilder.length() <= bitIndex - 1) {
stringBuilder.append('0');
}
stringBuilder.append('1');
}
return Integer.parseInt(stringBuilder.reverse().toString(), 2);

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Now that you know how it works try to do that with ^ and << operators

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants