?
Solved

Extracting a bit

Posted on 2003-02-20
9
Medium Priority
?
182 Views
Last Modified: 2012-05-04
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)
{
unsigned long temp, mask,counter,q,z;
z=n;
temp=0;
mask=1;
if(z>32)
z=32;
if(p==0)
{
  if ((x&mask)?1:0)
  temp=1;
  else
  temp=0;
}
else
{
mask<<=p;
if((p-n)<0)
{z=z-p-1;
      mask=0; mask<<=31;
    for(counter=1;counter<=z;counter++)
    {
    if ((x&mask)?1:0)
       q=1;
    else    
       q=0;
    temp=temp+q;
    temp<<=1;
    mask>>=1;
    }                                  /*temp = bits to right of p towards right end*/
    temp<<=(31-z-p-1);
    z=p+1;
    mask=1;
    mask<<=p;
    for (counter=1;counter<=z;counter++)
    {
    if ((x&mask)?1:0)
      q=1;
    else
      q=0;
    temp=temp+q;
    temp<<=1;
    mask>>=1;
    }
}
else
{
for(counter=1;counter<=z;counter++)
{
if ((x&mask)?1:0)
   q=1;
else
   q=0;
temp=temp+q;
temp<<=1;
mask>>=1;
}
}
}
hex_print(temp);

return 0;
}



0
Comment
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
  • Learn & ask questions
9 Comments
 

Author Comment

by:AqualostnC
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

by:akshayxx
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

by:
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.
Hint:  Start with no bits. "<< 1". Or in another LSB.

#3  Isolate the part you want.  Hint: "&"
0
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

Expert Comment

by:cybervagrant
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

by:TheBeaver
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

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

Expert Comment

by:cybervagrant
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

by:jmcg
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
accept answer by grg99.

PLEASE DO NOT ACCEPT THIS COMMENT AS AN ANSWER!

jmcg
EE Cleanup Volunteer
0

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

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…
Video by: Grant
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

765 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question