danilom

obtem bits

Sep 8th, 2011
129
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 0.55 KB | None | 0 0
  1. #include <stdio.h>
  2.  
  3. void dtob(unsigned int n);
  4.  
  5. int main(void) {
  6.        
  7.         dtob(6);
  8.         dtob(obtembits(6, 2, 1));
  9.         //printf("%d\n", );
  10.         return 0;
  11. }
  12.  
  13. int obtembits(x, p, n) unsigned int x, p, n; {//numera da direita pra esquerda
  14.         return (unsigned)x<<32-p>>32-n;
  15. }
  16.  
  17. int obtembits2(x, p, n) unsigned int x, p, n; {//numera da esquerda pra direita
  18.         return (unsigned)x<<p>>32-n;
  19. }
  20.  
  21. void dtob(unsigned int n) {
  22.         char i=0;
  23.         do{putchar((n>>31&1)+'0');}while((n<<=1)|++i<32);
  24.         putchar(10);
  25. }
Advertisement
Add Comment
Please, Sign In to add comment