Results 1 to 2 of 2

Thread: values of bits from 4th to 12th position is my required final data

  1. #1

    values of bits from 4th to 12th position is my required final data

    I need a C program which should do the following execution. An variable 'a' is declared which could be short, long, double,int. The input is taken from scanf by using "%x".

    Eg I have a 16 bit number say n = 65183 i.e in the compiler it stores the data as (1111 1110 1001 1111).

    Consider the 1 st bit as position 1 and last bit as 8th position. I pass value of x & y as 4 & 12. x represents starting location in the data 'n',& points to 4 bit of data.

    In the same way y =12 represents the end value and points to the 12th bit in the data.

    The values of bits from 4th to 12th position is my required final data.

    But the requirement is that, I have to copy only the 1st 9 bits (12th bit - 4bit) in the data because my requirement is from bit 4,5,6,7,8,9,10,11,12.

    These bits should be copied another variable. but the 4th of my selected data bit should start from LSB in the next variable. The rest of the data should be masked. retaining the required data. The value of n,x,y keeps on changing. So i need a generic formula in 1 step that gives me the output for any random input of n,x,y. The input data can be 8 bit 16bit 32bit or any long int or double.


  2. #2
    Junior Member
    Join Date
    Jun 2007
    Answers
    4

    Re: values of bits from 4th to 12th position is my required final data

    Hi,
    if u r looking for single step solution, then i dont think thier is as such predefined function. but it can be done with macro, here i have given simple solution, if it suits ur requirement then it its well n good, if not then u can precisely tailor it to ur requirement.

    #include <stdio.h>
    #include <math.h>

    #define requiredData(x,y,data) ((data & (int)(pow(2.0,y)-1))>>x)

    void main()
    {
    int x,data;
    double y;
    printf("\n enter x,y and data = ");
    scanf("%d %lf %x",&x,&y,&data);

    int finaldata = requiredData(x,y,data);
    printf("\ndata = %x \n",finaldata);
    }


Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
About us
Applying for a job can be a stressful and frustrating experience, especially for someone who has never done it before. Considering that you are competing for the position with a at least a dozen other applicants, it is imperative that you thoroughly prepare for the job interview, in order to stand a good chance of getting hired. That's where GeekInterview can help.
Interact