Skip to main content

Linguagem C_Exercicios Propostos Solucao_16

Page 1

Linguagem C 25.ª Edição Atualizada e Aumentada FCA Editora (c) 2025

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);

MYBIN.H 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)

© FCA

1


Turn static files into dynamic content formats.

Create a flipbook
Linguagem C_Exercicios Propostos Solucao_16 by Grupo Lidel - Issuu