1answer.
Ask question
Login Signup
Ask question
All categories
  • English
  • Mathematics
  • Social Studies
  • Business
  • History
  • Health
  • Geography
  • Biology
  • Physics
  • Chemistry
  • Computers and Technology
  • Arts
  • World Languages
  • Spanish
  • French
  • German
  • Advanced Placement (AP)
  • SAT
  • Medicine
  • Law
  • Engineering
dimaraw [331]
3 years ago
6

Given a positive integer, output its complement number. The complement strategy is to flip the bits of its binary representation

.Note: The given integer is guaranteed to fit within the range of a 32-bit signed integer. You could assume no leading zero bit in the integer’s binary representation.
Computers and Technology
1 answer:
EleoNora [17]3 years ago
5 0

Answer:

var findComplement = function(num) {

   

   var start = false;

       for (var i = 31; i >= 0; --i) {

           if (num & (1 << i)) {//find the leftmost hightest bit 1 and start from there

               start = true;

           }

           if (start) {

               

               num ^= (1 << i);

             

           }

       }

       return num;

};

var findComplement = function(num) {

   var bits = num.toString(2);

   var complement = '';

   for(var i=0;i<bits.length;i++)

   {

       complement += (bits[i]==1?0:1);

   }

   return parseInt(complement,2);

};

Explanation:

The javascript code above would accept a number in the variable complemnt and using the parseint keyword, it converts it to a binary value of that number.

Each bit is converted from the least to the most significant bit, then it is returned to the find compliment function and converted back to an integer.

You might be interested in
_____ is the software that protects you computer from harmful files, Trojan horses, and worms.
Bumek [7]
I think it's antivirus because Antivirus means to protect your computer from a deadly virus
4 0
4 years ago
Read 2 more answers
Debug the following program.
mamaluj [8]

Answer:

The debugged program is as follows:

A=2

B=2

For I=1 to 10

PRINT A

TEMP = A

A=B

B=TEMP+A

NEXT I

END

Explanation:

First, the value of B should be changed to 4 (because the second term of the sequence is 2

Next, change Display to Print because Q-basic uses the print keyword to display output

The sequence is not properly generated. So, I update that part of the program to:

<em>For I=1 to 10 </em>

<em>PRINT A </em>

<em>TEMP = A </em>

<em>A=B </em>

<em>B=TEMP+A </em>

<em>NEXT I </em>

<em />

Lastly, the loop is controlled by variable I (not X).

So change NEXT X to NEXT I

4 0
3 years ago
Question 12 There are ____ octets in an IP address.
Marta_Voda [28]
The standard IP convention used in today's network is IPv4. Taking for example one basic IP address we have 192.168.1.1 - The ff IP has 4 octets. An octet is a group of 8 bits on the sample, we have 4 Octets making up 32 a standard 32 bit address.
6 0
4 years ago
Setting your __________ will prevent you from having to type your name in every email. autograph designation name signature
makvit [3.9K]
Signature I think.........

6 0
3 years ago
Read 2 more answers
FILL IN THE BLANK
Gwar [14]

Answer:

search

Explanation:

4 0
3 years ago
Other questions:
  • Which of the following best describes the purpose of child labor laws?
    13·2 answers
  • What are the texture of metamorphic​
    12·1 answer
  • Encryption has a remarkably long and varied history. Spies have been using it to convey secret messages ever since there were se
    7·1 answer
  • The number 1 represent what state in binary code
    9·1 answer
  • Which property of a text element controls how the browser handles text that does not fit within the element box?
    13·1 answer
  • How much heat is needed to raise the temperature of 7g of water by 15oC?
    6·1 answer
  • What does the CFO of a company do
    14·1 answer
  • What file name would allow you to easily find and edit your document later in Word Online?
    14·2 answers
  • Give one of user responsibility of the internet you should follow in cyberethics.
    5·2 answers
  • What does a companys code of ethics cover
    8·1 answer
Add answer
Login
Not registered? Fast signup
Signup
Login Signup
Ask question!