Skip to content

Logic Quest

VLSI Interview Questions

  • Wave Diagram Analysis
  • Design Questions
  • Gate Level Questions
  • Some Coding
  • Contact us
  • Toggle search form

Invert Bits

Wrtie a function that gets a number and invert its bits.

For example:

INPUT  : 10 (1010)
OUTPUT : 5  (0101)

Answer

int invertBits(int num) {
	int count = 0;
	int invertBits = 0;
	while (num > 0) {
		if (num & 1)
			invertBits += 0 << count;
		else
			invertBits += 1 << count;
		num >>= 1;
		count++;
	}
	return invertBits;
}
INPUT  : 15 (1111)
OUTPUT : 0  (0000)

INPUT  : 24 (11000)
OUTPUT : 7  (00111)

INPUT  : 6  (110)
OUTPUT : 1  (001)

Next Question>>>

Comment (1) on “Invert Bits”

  1. TechEE says:
    March 14, 2022 at 7:36 pm

    I have a simple solution for this, based on the idea of 2’s complement.

    int reverse(int m)
    {
    int power = pow(2, log2(m) + 1);
    return power – m – 1;
    }

    Reply

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

Copyright © 2023 Logic Quest.

Powered by PressBook Blog WordPress theme