how do u find out the number of 1's in the binary
representation of a decimal number without converting it
into binary(i mean without dividing by 2 and finding out
the remainder)? three lines of c code s there it
seems...can anyone help
Answer Posted / krishna
The above solutions will not work for negative numbers.
if a negative number is right shifted, the most significant bit will become 1. so the number never becomes zero.
The following will work for both +ve and -ve numbers.
#include <stdio.h>
int main(int argc, char *argv[]) {
int count = 0;
int no = 3;
int x = 0x01;
if ( argc > 1) {
no = atoi(argv[1]);
}
while ( x != 0) {
if( no & x ) {
count++;
}
x <<= 1;
}
printf( "number: %d , no.of 1s in it: %d\n", no, count);
return 0;
}
| Is This Answer Correct ? | 1 Yes | 2 No |
Post New Answer View All Answers
Do you know the difference between exit() and _exit() function in c?
Should a function contain a return statement if it does not return a value?
Is it possible to have a function as a parameter in another function?
hw can we delete an internal node of binary search tree the internal node has child node..plz write progarm
How can a process change an environment variable in its caller?
What is a file descriptor in c?
Explain what is the difference between functions getch() and getche()?
How do I send escape sequences to control a terminal or other device?
main use of recursive function a) processing speed high b) reduce program length/reduce repeated statements c) if you do not, use iterative methods like, for, while or do-while d) all the above
please give me a VIRTUSA sample palcement papers.... you will only send TECHNICAL SECTION..... that is help for me Advance Thanks........................
How to declare a variable?
how to introdu5ce my self in serco
What are loops in c?
Write a C program to help a HiFi’s Restaurant automate its breakfast billing system. Your assignment should implement the following items: a. Show the customer the different breakfast items offered by the HiFi’s Restaurant. b. Allow the customer to select more than one item from the menu. c. Calculate and print the bill to the customer. d. Produce a report to present your complete program and show more sample output. Assume that the HiFi’s Restaurant offers the following breakfast menu: Plain Egg $2.50 Bacon and Egg $3.45 Muffin $2.20 French Toast $2.95 Fruit Basket $3.45 Cereal $0.70 Coffee $1.50 Tea $1.80
i have a written test for microland please give me test pattern