shaiko
Advanced Member level 5
- Joined
- Aug 20, 2011
- Messages
- 2,644
- Helped
- 303
- Reputation
- 608
- Reaction score
- 297
- Trophy points
- 1,363
- Activity points
- 18,302
int FindThird1(int reg)
{
int count = 0;
for (int i=0; i<32; i++)
{
count += (reg>>i)&1;
if (count == 3)
return i;
}
return -1;
}
The result of E1 >> E2 is E1 right-shifted E2 bit positions. If E1 has an unsigned type
or if E1 has a signed type and a nonnegative value, the value of the result is the integral
part of the quotient of E1 / 2E2. If E1 has a signed type and a negative value, the
resulting value is implementation-defined.
void _showbin(unsigned int x){
printf("%08x ",x);
for(unsigned i=8*sizeof(x),n=0;i--;){
putchar(((x >> i)&1) ? '1' : '0');
if(++n == 4){
n = 0;
putchar('.');
}
}
putchar('\t');
}
void showbin(int x){
_showbin(*((unsigned int *)&x));
}
#define Showbin(a) { showbin(a); printf("%s\n", #a); }
int main(void){
Showbin(1);
Showbin(1>>5);
Showbin(1<<5);
showbin((-1));
Showbin((-1)>>5);
Showbin(-2);
Showbin((-2)>>5);
Showbin((-2)<<5);
return 0;
}
00000001 0000.0000.0000.0000.0000.0000.0000.0001. 1
00000000 0000.0000.0000.0000.0000.0000.0000.0000. 1>>5
00000020 0000.0000.0000.0000.0000.0000.0010.0000. 1<<5
ffffffff 1111.1111.1111.1111.1111.1111.1111.1111.
ffffffff 1111.1111.1111.1111.1111.1111.1111.1111. (-1)>>5
fffffffe 1111.1111.1111.1111.1111.1111.1111.1110. -2
ffffffff 1111.1111.1111.1111.1111.1111.1111.1111. (-2)>>5
ffffffc0 1111.1111.1111.1111.1111.1111.1100.0000. (-2)<<5
1. The condition for exiting the for loop is i=32. On the other hand we have the return value statement:
if (count == 3)
return i;
So...when will the function exit? Will it loop over all 32 bits or will the
condition marked in red override this and cause the function to exit as soon as it's met?
2. What is the purpose of the "return -1" line ?
If you bit shift a negative value, you may not get the required results.
Thanks horace1,
So you're saying that the "return" statement overrides the loop condition?
Can you suggest something that will work for signed numbers?It is a bad programming behavior to shift signed numbers
It depends. What would you excpect by shifting a negative number?xenos,
Can you suggest something that will work for signed numbers?
It should be noted that the intended behaviour of the code in post #1 is unclear by itself, no only by the implementation depend behaviour of >> with signed numbers. So you must tell first which result you want for signed numbers. Are you considering the "one" bits of two's complement signed number representation?Can you suggest something that will work for signed numbers?
Yes, because none of the shifted-in bits gets the chance to be counted. Thanks for reminding the obvious.The behaviour of the code in post #1 is not dependent of the behaviour (arithmetic or logical) of the ">>" operator, provided that the type has 32 bits.
We use cookies and similar technologies for the following purposes:
Do you accept cookies and these technologies?
We use cookies and similar technologies for the following purposes:
Do you accept cookies and these technologies?