Question:
c++ determine bits in a 'long' using bitwise operations?
Derpette
2013-02-22 16:03:12 UTC
Hi

I need to do a bitwise operation using the function


unsigned int sizeOfLong(void)
{
// sizeOfLong determines the number of bits in the internal
// representation of a long
}


I need to be able to make this function determine the number of bits in the internal representation of a "long" using bitwise operations.

The output should be something like:
"The number of bits in the internal representation of a long is 32."

Can please someone give a few hints on what a "long" is and how to find it using bitwise operations? Thank you, I'm so lost
Four answers:
peteams
2013-02-22 17:02:59 UTC
Ratchetr is the closest, but chose to measure unsigned int rather than long.



Functionally identical code, but using the correct type, would be:



long powerOfTwo = 1;

int bits = 0;

while (powerOfTwo != 0)

{

powerOfTwo <<= 1;

bits++;

}



Note that bitwise << shift operator doesn't care about how negative numbers are represented so whether powerOfTwo is signed or unsigned is not material. All that matters is that you could the number of bit positions a single bit can occupy before it disappears.
husoski
2013-02-23 00:35:26 UTC
I'd start a long variable with a 1L, and a bit count of 1, then repeatedly double the long variable and increment the count until the result was no longer positive. That will work on all of the historical signed binary representations (sign&magnitude, ones-complement, in addition to the nearly universal twos-complement). Hypothetically, there might be encodings or overflow results that don't let that work, but it's unlikely.



Another way is to assume that the (unsigned long) type has the same number of bits as a long (not necessarily true, though I've never heard of an implementation where that was false) and the start with ~0UL as an unsigned long filled with 1 bits. Shift that right (or left) 1 bit at a time, counting how many shifts are needed to get a zero result.



That will work to get the bit size of unsigned longs on any implementation. Unlike the signed numbers, the format is known even though the size is not.



Using the headers and , you can get the results as:



2 + (int)l(og((double)LONG_MAX) / log(2.0)) .... bits in long, assuming a single sign bit; or

1 + (int)(log((double)ULONG_MAX) / log(2.0)) ... bits in unsigned long



That expression log(x)/log(2.0) finds the base-2 logarithm of a double value x. If you have a full C++11 version of then you can use 2+(int)log2((double)LONG_MAX) instead.
Ratchetr
2013-02-23 00:27:08 UTC
Hum. It would appear that JB didn't test his code before posting. Either that, or he has a machine where longs only have 1 bit.



Here is a version that has been tested. The basic idea here is to start with a value of 1, then left shift it until the 1 'falls off', leaving you with a value of 0 (which terminates the for loop):

unsigned int sizeOfLong(void)

{

    int bits = 0;

    for(unsigned int n=1;n;n<<=1,bits++);

    return bits;

}
James Bond
2013-02-23 00:15:30 UTC
unsigned int sizeOfLong(void)

{

long a=-1; //System uses 2s complment. In any 2s complement system -1 will be of form 11111111111

// sizeOfLong determines the number of bits in the internal



int nb=0;

while( a&1){nb++; a=a<<1;

}

return nb;

// representation of a long

}


This content was originally posted on Y! Answers, a Q&A website that shut down in 2021.
Continue reading on narkive:
Loading...