/* * @(#) set_bits.c - An implementation of 'setbits' function. * (c) 1995 Ivan Maidanski http://ivmai.chat.ru * Freeware program source. All rights reserved. ** * Language: ANSI C * Tested with: BSD GNU CC * Last modified: 1995-10-28 11:35:00 GMT+04:00 */ #include typedef unsigned int uint; uint setbits(uint x,uint p,uint n,uint y) { /* sets n bits into x at the position p from y */ return x & ~(~(~0 << n) << (p+1-n)) | (y & ~(~0 << n)) << (p+1-n); } main () { uint x,y,p,n; printf("Enter Hex x: "); scanf("%x",&x); printf("Enter Hex y: "); scanf("%x",&y); printf("Enter p: "); scanf("%d",&p); printf("Enter n: "); scanf("%d",&n); printf("Result: %x \n",setbits(x,p,n,y)); }