fork(2) download
  1. #include <stdio.h>
  2.  
  3. void sizedCopy(int size, int src, unsigned long *dst)
  4. {
  5. int chk;
  6. int loop;
  7. int shift;
  8.  
  9. shift = size - 1;
  10. chk = (0x01 << (size - 1)); // from MSB to LSB
  11.  
  12. for(loop=0; loop<size; loop++) {
  13. if ( (src & chk) > 0 ) {
  14. *dst |= (0x01 << shift);
  15. } else {
  16. *dst &= ~(0x01 << shift);
  17. }
  18. chk = (chk >> 1);
  19. shift--;
  20. }
  21. }
  22.  
  23. int main(void) {
  24. unsigned long ulval = 0;
  25. sizedCopy(3, 0x35, &ulval); // 0x35 = 110101b
  26. printf("0x%X", ulval); // answer: 0x5
  27. return 0;
  28. }
  29.  
Success #stdin #stdout 0s 2160KB
stdin
Standard input is empty
stdout
0x5