Saturday, November 16, 2013



Sum of Bit Indices.

Difficulty (1/5).

In some programming languages, such as C and Java, an integer is stored as a 32 bit quantity. For example, the integer 267 is represented by the following bits:
0000 0000 0000 0000 0000 0001 0000 1011
The bits of the integer are numbered from 0 to 31, with rightmost bit being considered bit 0, often called the "low order bit". The leftmost bit is considered to be bit 31, often called the "high order bit", or "sign bit". In the example above, bits 0, 1, 3, and 8 are 1 bits.

Write a program that will take an integer on the command line and print the sum of the bit indices that have a 1 bit in them. For example, given the input:
% java SumBitIndices 267
your program should calculate and print the value 12 (0+1+3+8).

To test your program, calculate the sum of the bit indices of the number 1265.

CS1 Deadline: 12/6/2013
Use: handin cs1113 extra5 SumBitIndices.java

See also: http://graphics.stanford.edu/~seander/bithacks.html




No comments:

Post a Comment