16
EXERCÍCIOS PROPOSTOS – SOLUÇÃO
1: #pragma once 2: #include <limits.h>
3:
4: typedef unsigned long long BIGINT; 5: typedef unsigned short int BOOLEAN; 6: typedef BIGINT BIT_INT; 7:
8: #define TRUE ((BOOLEAN) 1)
9: #define FALSE ((BOOLEAN) 0)
10: #define ZERO ((BIT_INT) 0)
11: #define ONE ((BIT_INT) 1)
12: #define BIGINT_SIZE (CHAR_BIT * sizeof(BIGINT))
13:
14: BIT_INT getBit(BIGINT value, int n_bit);
15: void setBit(BIGINT *value, int n_bit, BIT_INT bit_value);
16:
17: BIGINT applyOR(BIGINT value1, BIGINT value2);
18: BIGINT applyAND(BIGINT value1, BIGINT value2);
19: BIGINT applyXOR(BIGINT value1, BIGINT value2);
20: BIGINT applyNOT(BIGINT value);
1: #include <stdio.h>
2: #include <stdlib.h>
3: #include "mybin.h"
4:
5: BIT_INT getBit(BIGINT value, int n_bit)
6: { 7: return (value >> n_bit) & ONE; 8: } 9:
10: void setBit(BIGINT *value, int n_bit, BIT_INT bit_value) 11: { 12: BIGINT mask = ONE << n_bit; 13:
14: if (bit_value == ZERO)
15: *value &= (~mask); 16: else // ONE 17: *value |= mask; 18: }
19:
20: BIGINT add1(BIGINT value)
21: {
22: BIGINT res = value; 23: BIGINT e_vai = ONE; // Adicionar 1 (como se fosse e vai um)
24:
25: for (int n_bit = 0; n_bit < BIGINT_SIZE; n_bit++)
26: {
27: if (e_vai==ZERO) break; // o número está completo 28:
29: BIT_INT cur_bit_value = getBit(value, n_bit);
30:
31: if (cur_bit_value==ZERO)
32: setBit(&res, n_bit, ONE);
33: else
34: setBit(&res, n_bit, ZERO);
35: e_vai = (cur_bit_value == ONE);
36:
37: }
38:
39: return res;
40: }
41:
42: // Função local genérica para aplicar os operadores binários bit-a-bit
43: static BIGINT applyOperation(BIGINT value1, BIGINT value2, char op)
44: {
45: BIGINT res=0;
46: BIT_INT bit_value;
47:
48: for(int n_bit=0; n_bit<BIGINT_SIZE; n_bit++)
49: {
50: switch(op)
51: {
52: case '|' : bit_value = getBit(value1, n_bit) | getBit(value2, n_bit); break; 53: case '&' : bit_value = getBit(value1, n_bit) & getBit(value2, n_bit); break; 54: case '^' : bit_value = getBit(value1, n_bit) ^ getBit(value2, n_bit); break;
55: }
56: setBit(&res, n_bit, bit_value);
57: }
58:
59: return res;
60: }
61:
62:
63: BIGINT applyOR(BIGINT value1, BIGINT value2)
64: {
65: return applyOperation(value1, value2, '|');
66: }
67:
68: BIGINT applyAND(BIGINT value1, BIGINT value2)
69: {
70: return applyOperation(value1, value2, '&');
71: }
72:
73: BIGINT applyXOR(BIGINT value1, BIGINT value2)
74: {
75: return applyOperation(value1, value2, '^'); 76: }
77:
78: BIGINT applyNOT(BIGINT value)
79: {
80: return applyXOR(value, 0xFFFFFFFFFFFFFFFFll); 81: }
82:
83: void printBinary(BIGINT value)
84: {
85: for (int i=CHAR_BIT*sizeof(value)-1; i>=0; i--)
86: {
87: putchar(getBit(value, i) + '0');
88: if (i%CHAR_BIT==0)
89: putchar(' ');
90: else
91: if (i%(CHAR_BIT/2)==0) putchar('-'); 92: } 93: } 94:
95:
96: int main(void)
97: { 98:
99: BIGINT value = 0; 100: printf("Imprimir o valor inicial (ZERO)\n"); 101: printBinary(value); printf("\n\n"); 102:
103: printf("Colocar a 1 (ONE) o MSB e o LSB\n"); 104: setBit(&value, 0, ONE); 105: setBit(&value, BIGINT_SIZE-1, ONE); 106: printBinary(value); printf("\n\n"); 107:
108: printf("Colocar a 0 (ZERO) o MSB e o LSB\n"); 109: setBit(&value, 0, ZERO); 110: setBit(&value, BIGINT_SIZE-1, ZERO); 111: printBinary(value); printf("\n\n"); 112:
113: printf("Colocar todos os bits em posições par a 1\n"); 114: for (int i=0; i<BIGINT_SIZE; i+=2) 115: setBit(&value, i, ONE); 116: printBinary(value); printf("\n\n"); 117:
118: printf("Colocar apenas os bits em posições impar a 1\n"); 119: for (int i=0; i<BIGINT_SIZE; i++)
120: setBit(&value, i, i%2==1? ONE : ZERO); 121: printBinary(value); printf("\n\n"); 122:
123: BIGINT value2 = 0; 124: printf("Realizar 10 somas de 1, a partir do número 0\n"); 125: printBinary(value2); printf("\n"); 126: for (int i=1; i<=10; i++)
127: {
128: value2 = add1(value2); 129: printBinary(value2); printf("\n");
130: }
131: printf("\n");
132:
133: value = (long long) 0x2a7fc122d16a9e0c; // um valor qualquer 134: BIGINT mask = 0xFF00F00F0FF000FF; 135:
136: printBinary(value); printf(" value\n"); 137: printBinary(mask); printf(" mask\n");
138: printBinary(applyOR(value, mask)); printf(" value OR mask\n\n"); 139:
140: printBinary(value); printf(" value\n"); 141: printBinary(mask); printf(" mask\n"); 142: printBinary(applyAND(value, mask)); printf(" value AND mask\n\n"); 143:
144: printBinary(value); printf(" value\n"); 145: printBinary(mask); printf(" mask\n"); 146: printBinary(applyXOR(value, mask)); printf(" value XOR mask\n\n"); 147:
148: printBinary(value); printf(" value\n"); 149: printBinary(applyNOT(value)); printf(" NOT value\n"); 150:
151: return 0; 152: }