Solved

# Extracting a bit

Posted on 2003-02-20
Medium Priority
182 Views
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
Question by:AqualostnC
[X]
###### Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

• Help others & share knowledge
• Earn cash & points

Author Comment

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

LVL 8

Expert Comment

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

LVL 22

Accepted Solution

grg99 earned 400 total points
ID: 7992868
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

Expert Comment

ID: 7996365
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

LVL 1

Expert Comment

ID: 7996665
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

Expert Comment

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

0

Expert Comment

ID: 7999779
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

LVL 20

Expert Comment

ID: 10011233
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

jmcg
EE Cleanup Volunteer
0

## Featured Post

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Preface I don't like visual development tools that are supposed to write a program for me. Even if it is Xcode and I can use Interface Builder. Yes, it is a perfect tool and has helped me a lot, mainly, in the beginning, when my programs were smallâ€¦
Summary: This tutorial covers some basics of pointer, pointer arithmetic and function pointer. What is a pointer: A pointer is a variable which holds an address.Â This address might be address of another variable/address of devices/address of fuâ€¦
The goal of this video is to provide viewers with basic examples to understand and use nested-loops in the C programming language.
The goal of this video is to provide viewers with basic examples to understand opening and reading files in the C programming language.
###### Suggested Courses
Course of the Month8 days, 3 hours left to enroll