2.6.c 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  1. #include <stdio.h>
  2. unsigned getbits(unsigned x, int p, int n);
  3. unsigned int setbits(unsigned int x,unsigned int p,unsigned int n,unsigned int y);
  4. #define MAGIC_NUMBER 33091
  5. void shotgunPrintUint(const char name[], unsigned int in);
  6. int main()
  7. {
  8. shotgunPrintUint("return ",setbits(0xdeadbeef,12,5,0x00000000));
  9. // printf("i:%u \t temp:%x \n",-1,temp);
  10. /** shifting 32 bits out is an overflow condition .*/
  11. // unsigned int temp = ~0;
  12. // unsigned int temp2=temp<<32;
  13. // printf("\t temp:%x \n",temp2);
  14. return(0);
  15. }
  16. unsigned int setbits(unsigned int x,unsigned int p,unsigned int n,unsigned int y)
  17. {
  18. //printf(" x : %0#10x : p %d : n %d \n",x,p,n);
  19. unsigned int left = getbits(x,p+n,32-(n+p)); //if position is zero...we just want middle case to handle it.
  20. //shotgunPrintUint("left ",left);
  21. unsigned int left_out = ((n+p)<32) ? left << (n+p) :0 ;
  22. //left is in the right spot
  23. //shotgunPrintUint("left_out",left_out);
  24. unsigned int middle = getbits(y,p,n);
  25. //shotgunPrintUint("middle ",middle);
  26. unsigned int middle_out = middle << p ;
  27. //shotgunPrintUint("middle_out",middle_out);
  28. //shotgunPrintUint("middle_out",middle_out);
  29. unsigned int right_out = getbits(x,0,p); // | getbits(y,p,n);
  30. //shotgunPrintUint("right_out ",right_out);
  31. //printf(" right : %x \n ",right);
  32. // unsigned int right_out = right >> (n+p);
  33. //shotgunPrintUint("right_out",right_out);
  34. return left_out | middle_out | right_out;
  35. }
  36. /* the problem here is this function is in fact broken due to ~0 << n not
  37. being valid C anymore.
  38. KNR Bullshit: ~0 << n is not defined in ANSI C
  39. KNR getbits: get n bits from position p
  40. */
  41. /*
  42. even if it is in K&R C
  43. */
  44. /* getbits: get n bits from position p */
  45. unsigned getbits(unsigned x, int p, int n)
  46. {
  47. // shotgunPrintUint("test me",0x1);
  48. // shotgunPrintUint("test me",0x0);
  49. //printf ("getbits x: %0#10x ,p: %d ,n: %d \n",x,p,n);
  50. unsigned int leftmask,rightmask;
  51. leftmask = rightmask = ~0;
  52. //shotgunPrintUint("maxmask ",leftmask);
  53. leftmask = leftmask << p; //find position p
  54. // shotgunPrintUint("leftmask ",leftmask);
  55. int shiftme = 32-(p+n);
  56. rightmask = (shiftme < 32) ? rightmask >> shiftme : 0; //find position p
  57. //shotgunPrintUint("rightmask ",rightmask);
  58. //shotgunPrintUint("shiftme ",shiftme);
  59. unsigned int finalmask = leftmask & rightmask;
  60. unsigned int data = x & finalmask;
  61. // shotgunPrintUint("mask ",finalmask);
  62. // shotgunPrintUint("data",data);
  63. unsigned int outdata = data >> p;
  64. //shotgunPrintUint("outdata",outdata);
  65. //unsigned int outdata = (x >> (p-n));
  66. // printf(" mask : %x \n ",mask);
  67. //shotgunPrintUint("outdata",outdata);
  68. return outdata; // (x >> (p-n)) & mask;
  69. }
  70. /**
  71. caution if you printf, padded with 0's, a hex number of exactly 0 it will print
  72. as 0000000000 not 0x00000000
  73. */
  74. /**
  75. drop me in if you don't want to use a debugger like GDB to shotgun-debug
  76. print the value of a uint
  77. */
  78. void shotgunPrintUint(const char name[], unsigned int in)
  79. {
  80. printf(name,23423);
  81. printf("\t\t: %0#10x \n",in);
  82. return;
  83. }