write a program to find out number of on bits in a number?
Answer Posted / vivek
int setbit=1; //Lets start checking from first bit
int numBitSet=0; //Number of bits set in the number
while(setbit>0)
{
if(number&setbit) //bit wise and
numBitSet++;
setbit=setbit<<1;
}
| Is This Answer Correct ? | 23 Yes | 12 No |
Post New Answer View All Answers
Can we change the value of static variable in c?
What are pointers?
Synonymous with pointer array a) character array b) ragged array c) multiple array d) none
Write a program to replace n bits from the position p of the bit representation of an inputted character x with the one's complement. Method invertBit takes 3 parameters x as input character, p as position and n as the number of positions from p. Replace n bits from pth position in 8 bit character x. Then return the characters by inverting the bits.
What is the return type of sizeof?
Should I use symbolic names like true and false for boolean constants, or plain 1 and 0?
How can I write data files which can be read on other machines with different word size, byte order, or floating point formats?
how should functions be apportioned among source files?
What is double pointer?
Explain is it better to bitshift a value than to multiply by 2?
What is difference between far and near pointers?
What's the difference between constant char *p and char * constant p?
How can I manipulate strings of multibyte characters?
How can you avoid including a header more than once?
write an interactive C program that will encode or decode a line of text.To encode a line of text,proceed as follows. 1.convert each character,including blank spaces,to its ASCII equivalent. 2.Generate a positive random integer.add this integer to the ASCII equivalent of each character.The same random integer will be used for the entire line of text. 3.Suppose that N1 represents the lowest permissible value in the ASCII code,and N2 represents the highest permissible value.If the number obtained in step 2 above(i.e.,the original ASCII equivalent plus the random integer)exceeds N2,then subtract the largest possible multiple of N2 from this number,and add the remainder to N1.Hence the encoded number will always fall between N1 and N2,and will therefore always represent some ASCII character. 4.Dislay the characters that correspond to the encoded ASCII values. The procedure is reversed when decoding a line of text.Be certain,however,that the same random number is used in decodingas was used in encoding.