Sunday, 15 May 2011

binary - interpreting 1 bit counting -



binary - interpreting 1 bit counting -

write function named bitcount() in bitcount.c returns number of 1-bits in binary representation of unsigned integer argument. remember fill in identification info , run completed programme verify correctness.

/* name: lab section time: */ #include <stdio.h> int bitcount (unsigned int n); int main ( ) { printf ("# 1-bits in base of operations 2 representation of %u = %d, should 0\n", 0, bitcount (0)); printf ("# 1-bits in base of operations 2 representation of %u = %d, should 1\n", 1, bitcount (1)); printf ("# 1-bits in base of operations 2 representation of %u = %d, should 16\n", 2863311530u, bitcount (2863311530u)); printf ("# 1-bits in base of operations 2 representation of %u = %d, should 1\n", 536870912, bitcount (536870912)); printf ("# 1-bits in base of operations 2 representation of %u = %d, should 32\n", 4294967295u, bitcount (4294967295u)); homecoming 0; } int bitcount (unsigned int n) { /* code here */ }

can help me understand what's asking? bitcount supposed convert decimal inputted binary, , count number of 1's?

there no "decimal" input function, it's beingness passed plain (unsigned, seems) numbers. stored in binary on typical computers.

i guess homecoming these values, instance:

bitcount(0) -> 0 (since 0 has no bits set) bitcount(1) -> 1 (since 1 12 in binary) bitcount(2) -> 1 (since 2 102 in binary) bitcount(3) -> 2 (since 3 112 in binary)

it doesn't matter base of operations number given in in source code function called, that'll converted binary 1 time programme runs. can phone call bitcount(01) (octal) or bitcount(0x80), it's still getting unsigned int value can assumed stored in binary.

a recursive algorithm bitcount(x) such:

if x 0, homecoming 0 if x 1, homecoming 1 return bitcount(x mod 2) + bitcount(x / 2)

note pseudocode doesn't assume number x stored in particular way (be binary or whatever), works on number itself. literals in pseudocode in decimal, that's notation.

binary

No comments:

Post a Comment