Golgappa.net | Golgappa.org | BagIndia.net | BodyIndia.Com | CabIndia.net | CarsBikes.net | CarsBikes.org | CashIndia.net | ConsumerIndia.net | CookingIndia.net | DataIndia.net | DealIndia.net | EmailIndia.net | FirstTablet.com | FirstTourist.com | ForsaleIndia.net | IndiaBody.Com | IndiaCab.net | IndiaCash.net | IndiaModel.net | KidForum.net | OfficeIndia.net | PaysIndia.com | RestaurantIndia.net | RestaurantsIndia.net | SaleForum.net | SellForum.net | SoldIndia.com | StarIndia.net | TomatoCab.com | TomatoCabs.com | TownIndia.com
Interested to Buy Any Domain ? << Click Here >> for more details...


CopyBits(x,p,n,y)
copy n LSBs from y to x starting LSB at 'p'th position.

Answers were Sorted based on User's Feedback



CopyBits(x,p,n,y) copy n LSBs from y to x starting LSB at 'p'th position...

Answer / mohammed sardar

Sorry above answer ; I did a mistake
t=0;
for(i=n; i>0; i--)
{
t|=(1<<p);
p++;
}
x=x&~t
t=t&y;
x=x|t;

Is This Answer Correct ?    1 Yes 0 No

CopyBits(x,p,n,y) copy n LSBs from y to x starting LSB at 'p'th position...

Answer / yash

t =((~(~0<<n))<<p)
x = x & ~t;
y = y & t;
x=x|y;

Is This Answer Correct ?    1 Yes 0 No

CopyBits(x,p,n,y) copy n LSBs from y to x starting LSB at 'p'th position...

Answer / angad

t=0;
for(i=n; i>0; i--)
{
t|=(1<<p);
p++;
}
x=x&~t
t=t&(y<<p);
x=x|t;
}

x=10100110
y=11110111
let,p=3,n=4

after for loop, t=01111000 - mask for the n(=4) bits starting from the p(=3) bit that need to be altered in x

x=x&~t;
x =10100110
~t=10000111
ANDing clears the 4 bits to zero(bits 3-6)
x=1 0000 111

we need to extract the 1st n(=4) bits out of y , and shift them left to align them against the n(=4) bits of x we need to alter, therefore, left shift y by p(=3)
t=t&(y<<p)

y<<p = 1 0111 000
t = 0 1111 000
AND=>t = 0 0111 000

now x = 1 0000 111
t = 0 0111 000

x=x|t =>1 0111 111

Is This Answer Correct ?    1 Yes 0 No

CopyBits(x,p,n,y) copy n LSBs from y to x starting LSB at 'p'th position...

Answer / mohammed sardar

t=0;
for(i=n; i>0; i--)
{
t|=(1<<p);
p++;
}
t=t&y;
x=x&t;

Is This Answer Correct ?    0 Yes 0 No

CopyBits(x,p,n,y) copy n LSBs from y to x starting LSB at 'p'th position...

Answer / rakesh

It should be p--, not p++. for example y = 217 (11011001)
and you want to extract the least 4 SB then n = 4 and p = 3
(remember the number start from 0 to 7 in memory). After
executing this you will get x = 9 i.e 1001.

t=0;
for(i=n; i>0; i--)
{
t |= (1<<p);
p--;
}
x=x&~t;
t=t&y;
x=x|t;

Is This Answer Correct ?    0 Yes 0 No

CopyBits(x,p,n,y) copy n LSBs from y to x starting LSB at 'p'th position...

Answer / vadivel t

Hi,
The below code ll giv desired o/p.

#include<stdio.h>
#include<conio.h>

int main()
{
int x, y, n , p, i, j, temp;
printf("ENTER X, Y, NO OF BITS AND BIT POSITION: \n");
scanf("%d %d %d %d",&x, &y, &n, &p);
for(i = p, j = 0; i < n+p; i++, j++)
{
if(x & (0x01 << i))
x = x^(0x01<<i);
temp = y & (0x01<<j) ? 1 : 0;
x = x | (temp << i-1);
}
printf("VALUE OF X:%d \n",x);
getch();
}

Is This Answer Correct ?    0 Yes 0 No

CopyBits(x,p,n,y) copy n LSBs from y to x starting LSB at 'p'th position...

Answer / vadivel t

Hi,
Small bug is there in the above code, which i have posted.
But the same has been resolved here.

#include<stdio.h>
#include<conio.h>

int main()
{
int x, y, n , p, i, j, temp;
printf("ENTER X, Y, NO OF BITS AND BIT POSITION: \n");
scanf("%d %d %d %d",&x, &y, &n, &p);
for(i = p, j = 0; i < n+p; i++, j++)
{
if(x & (0x01 << i-1))
x = x^(0x01 << i-1);
temp = y & (0x01 << j) ? 1 : 0;
x = x | (temp << i-1);
}
printf("VALUE OF X:%d \n",x);
getch();
}

Is This Answer Correct ?    0 Yes 0 No

CopyBits(x,p,n,y) copy n LSBs from y to x starting LSB at 'p'th position...

Answer / vino

can you pls explain the above logic?

Is This Answer Correct ?    0 Yes 0 No

CopyBits(x,p,n,y) copy n LSBs from y to x starting LSB at 'p'th position...

Answer / intfail

all the above answers are wrong...
never use loops. immediate rejection

CopyBits(x, p, n, y)

First get n bits from pos p from Y

bitsFromy = y >> (p-n+1) & (~(~0<<n))

Now, get a mask such that we can 0 out bits in x at pos p and n bits to the right

startpos = p -n +1

create a mask from (startpos, p)
mask = (~0 << p - startpos +1)<<startpos | ~(~0 << startpos)

Now, 0 out the the bits in the locations (starpos, p) in x
and apply the bits extracted from y
x = (x & mask) | (bitsFromy << startpos)

that is all it takes.

Is This Answer Correct ?    0 Yes 0 No

Post New Answer

More C Interview Questions

what is the advantage of using SEMAPHORES to ORDINARY VARIABLES???

2 Answers   NSN,


what is the difference between #include<> and #include”…”?

5 Answers  


explain about storage of union elements.

2 Answers   ABC, Bosch,


What is the difference b/w Structure & Union?

3 Answers  


What is variable and explain rules to declare variable in c?

0 Answers  


Differentiate call by value and call by reference?

0 Answers   Cyient,


What are keywords in c with examples?

0 Answers  


how to go with this?

1 Answers   Wipro,


Write a programme using structure that create a record of students. The user allow to add a record and delete a record and also show the records in ascending order.

0 Answers   Sikkim Manipal University,


What are shell structures used for?

0 Answers  


What are the differences between new and malloc in C?

0 Answers   Amazon,


Given an array of numbers, except for one number all the others occur twice. Give an algorithm to find that number which occurs only once in the array.

6 Answers  


Categories