Created
August 20, 2012 01:37
-
-
Save thiagoperes/3399154 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <stdio.h> | |
int mul (int a, int b) { | |
int p = 0; | |
while (b) { | |
if ((b&1) == 1) | |
p += a; | |
a <<= 1; | |
b >>= 1; | |
} | |
return p; | |
} | |
int isPrio(unsigned int pacote) | |
{ | |
return (pacote>>12)&1; | |
} | |
int getSeq(unsigned int pacote) | |
{ | |
return (pacote>>27)&0xf; | |
} | |
unsigned int incrSeq(unsigned int pacote) | |
{ | |
return 0; | |
} | |
unsigned int _numberOfbits(unsigned int val) | |
{ | |
val &= 0x7fffffff; // seta 0 no bit de paridade | |
int n = 0; | |
while (val) { | |
++n; | |
val &= val - 1; | |
} | |
return n; | |
} | |
unsigned int setParity(unsigned int pacote) | |
{ | |
printf("pac:\t%x\tbits\t%d\n", pacote, _numberOfbits(pacote)); | |
if ((_numberOfbits(pacote)&1) == 1) { | |
// tratar par | |
return pacote&0x7fffffff; // seta 0 | |
} | |
else { | |
// tratar impar | |
return pacote|0x80000000; // seta 1 | |
} | |
} | |
int main() | |
{ | |
unsigned int pacote1 = 0x7801E3A2; // 0111 1000 0000 0001 1110 0100 1010 0010 | |
unsigned int pacote2 = 0x0801FBA2; // 0000 1000 0000 0001 1111 1011 1010 0010 | |
unsigned int pacote3 = 0xF000000F; // pacote impar, com bit de paridade 1 | |
// Exercicio 1 | |
printf("%d\n", mul(3,3)); | |
printf("%d\n", mul(3,0)); | |
printf("%d\n\n", mul(4,2)); | |
// Exercicio 2-1 | |
printf("pac1 = %x\n", pacote1); | |
printf("pac2 = %x\n", pacote2); | |
printf("pac3 = %x\n\n", pacote3); | |
printf("prio pac1 = %s\n", isPrio(pacote1) ? "S" : "N"); | |
printf("prio pac2 = %s\n\n", isPrio(pacote2) ? "S" : "N"); | |
// Exercicio 2-2 | |
printf("seq pac1 = %d\n", getSeq(pacote1)); | |
printf("seq pac2 = %d\n\n", getSeq(pacote2)); | |
// Exercicio 2-3 | |
printf("ats pac1 = %d\n", getSeq(pacote1)); | |
printf("dps pac1 = %d\n", getSeq(pacote1)); | |
printf("set pac1 = %x\n", incrSeq(pacote1)); | |
printf("set pac2 = %x\n", incrSeq(pacote2)); | |
printf("ats pac2 = %d\n", getSeq(pacote2)); | |
printf("dps pac2 = %d\n\n", getSeq(pacote2)); | |
// Exercício 2-4 | |
printf("parity pac1 = %x\n", setParity(pacote1)); | |
printf("parity pac2 = %x\n", setParity(pacote2)); | |
printf("parity pac3 = %x\n", setParity(pacote3)); | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment