• Status: Solved
• Priority: Medium
• Security: Public
• Views: 183

Extracting a bit

Write a function to extract from x the right-most n bits that begin at position p. If the number of bits n extends beyond the end of the object, extract the bits from the left side, (wrap-around). Thus, x [7:16] yields x[7:8] & x[31:8] if x is an unsigned long object and where & stands for bit-vector concatenation. For example 0xABCDEF12[7:16] yields 0x12AB. If n exceeds the number of bits in the object, stop when you wrap around to where you started. The function prototype should be as follows:

unsigned long getfield(unsigned long x, int p, int n);

here is what i've done, it won't run
unsigned long getfield(unsigned long x,int p,int n)
{
z=n;
temp=0;
if(z>32)
z=32;
if(p==0)
{
temp=1;
else
temp=0;
}
else
{
if((p-n)<0)
{z=z-p-1;
for(counter=1;counter<=z;counter++)
{
q=1;
else
q=0;
temp=temp+q;
temp<<=1;
}                                  /*temp = bits to right of p towards right end*/
temp<<=(31-z-p-1);
z=p+1;
for (counter=1;counter<=z;counter++)
{
q=1;
else
q=0;
temp=temp+q;
temp<<=1;
}
}
else
{
for(counter=1;counter<=z;counter++)
{
q=1;
else
q=0;
temp=temp+q;
temp<<=1;
}
}
}
hex_print(temp);

return 0;
}

0
AqualostnC
1 Solution

Author Commented:
Is there any way possible to get an answer before12 pm pacific standard time friday 2/21/03???
0

Commented:
it ispossible .. but why do u need full implementation .. did u give it an attempt .. this sounds like an assignment..
0

Commented:
Well, it sounds like an assignment, so no code, but we can give you  some hints.

First break it down into smaller steps:

#1:  Position the data with the bits you want in the low part of the word.  Hint:   ">>"

#2:  Build a bitmask of the number of bits you want.

#3  Isolate the part you want.  Hint: "&"
0

Commented:
unsigned long getfield(unsigned long x, int p, int n)  {

int a;

a = ( ( p + 1 - n ) >  0 ? p + 1 - n : n - 1 - p );  // Get prepared //

if ( ( p - n ) < 1 )

x = ( x << a ) | ( x >> ( 32 - a ) );   // Rotate left if necessary //

if ( ( p - n ) > 1 )

x = x >> a;                            // Shift right if necessary //

x = x << ( 32 - n ); x = x >> ( 32 - n );  // Bam! Bam! Thank you... //

return x;                                     // Done //

}
0

Commented:
unsigned long getfield(unsigned long x, int p, int n)
{
unsigned long a = x;                  // Case of 0, 32

// Lets handle the rotation into the right position first
while( p )
{
a = a >> 1;

if( x & 1 )
a |= 0x80000000;                 // Do wrap (just in case)

x = x>>1;
p--;
}
// We now have x in the correct position...

// Now to extract n bits...
return a & ((1 << n)-1);
}
0

Commented:
Yours is interesting, but mine works, even for case 0,32 :)

0

Commented:
Heres a test shell, just make the substitute the functions.

#include <stdio.h>

unsigned long getfield(unsigned long x, int p, int n)  {

int a;

a = ( ( p + 1 - n ) > 0 ? p + 1 - n : n - 1 - p );  // Get set, //

if ( ( p - n ) < 1 )

x = ( x << a ) | ( x >> ( 32 - a ) );   // Turn to the left, //

if ( ( p - n ) > 1 )

x = x >> a;                            // Lean to the right, //

x = x << ( 32 - n ); x = x >> ( 32 - n );  // Bam! Bam! //

return x;

}

void main ()  {

unsigned long num;

int p, n;

for ( ; ; )

{
printf("Enter a 32 bit Hex value and a position and an amount:\n\n");
scanf ( "%lx %i %i", &num, &p, &n );
num = getfield( num, p, n );
printf("\n%X \n\n", num);
}

return;

}
0

OwnerCommented:
Nothing has happened on this question in over 10 months. It's time for cleanup!

My recommendation, which I will post in the Cleanup topic area, is to