PDA

View Full Version : C to TB question



Michael Clease
03-06-2010, 10:18
I was converting some C code and hoping I was converting the following code correctly can someone confirm.


{
int n;
for( n = 32; !(x & 0x80000000) && (n > 0); -- n ) x <<= 1;
return n;
}


Local n As Integer Value N = 32

While Not ( ANDB(x, &h80000000)) And (n > 0)
Decr n
SHIFT LEFT x,1
Wend

Function = N

Thanks
Mike

Petr Schreiber
03-06-2010, 11:28
Hi Michael,

I usually translate 0xNNN... hexanumbers like &h0NNN..., that makes them unsigned. But in this case it does not seem to do difference.

But I think your code works, except the first line, where your duplication of N makes trouble.
I would write it as:


Local n As Integer = 32

While Not ( ANDB(x, &h080000000)) And (n > 0)
Decr n
SHIFT LEFT x,1
Wend

Return N


... as ThinBasic has return as well.


Petr

Michael Clease
03-06-2010, 12:11
I missed the duplication of N (it was late lastnight)

Thanks Petr