Seabra Posted November 30, 2019 at 03:05 PM Report Share #616747 Posted November 30, 2019 at 03:05 PM Estou farto de dar voltas com este código e não funciona bem. O que pretendo é que faça o deslocamento de bits dentro do array. Apenas desloca entre bytes e não faz o carry do byte anterior. Alguém me pode ajudar? #include <stdio.h> #include <stdlib.h> void imprime(char texto); void shift_right(unsigned char *ar, int size, int shift); int main() { unsigned short alfabeto[8] ={0b11000001, 0b11000001, 0b11000000, 0b10000000, 0b10000000, 0b10000000, 0b10000000, 0b10000000}; int a,b; char Shift_data=1; shift_right(&alfabeto[0], 8, 7); //imprime array for (a=0; a<8; a++) { imprime(alfabeto[a]); } return 0; } unsigned short alfabeto[8] ={0b11000001, 0b11000001, 0b11000000, 0b10000000, 0b10000000, 0b10000000, 0b10000000, 0b10000000}; shift_right(&alfabeto[0], 8, 1); void shift_right(unsigned char *ar, int size, int shift) { char carry = 0; while (shift--) { for (int i = size - 1; i >= 0; --i) { char next = (ar[i] & 1) ? 0x80 : 0; ar[i] = carry | (ar[i] >> 1); carry = next; } } } void imprime(char texto) { int i; for (i = 0; i < 8; i++) { printf("%d", !!((texto << i) & 0x80)); } printf("\n"); } Link to comment Share on other sites More sharing options...
PsySc0rpi0n Posted December 1, 2019 at 06:25 PM Report Share #616751 Posted December 1, 2019 at 06:25 PM (edited) O teu cóigo está um pouco confuso. Porque declaras duas vezes a mesma variável alfabeo[8]? Depois chamas a função shift_right() fora da função main()... Não percebo... Edited; Ainda me dei ao trabalho de copiar o teu code e compilá-lo. Entre outras mensagens de erro (que são capazes de ser só do meu compilador), obtenho os seguintes erros: Quote warning: passing argument 1 of ‘shift_right’ from incompatible pointer type [-Wincompatible-pointer-types] shift_right(&alfabeto[0], 8, 7); ^~~~~~~~~~~~ shiftr.c:5:33: note: expected ‘unsigned char *’ but argument is of type ‘short unsigned int *’ void shift_right(unsigned char *ar, int size, int shift); ~~~~~~~~~~~~~~~^~ shiftr.c:11:6: warning: unused variable ‘Shift_data’ [-Wunused-variable] char Shift_data=1; ^~~~~~~~~~ shiftr.c:10:7: warning: unused variable ‘b’ [-Wunused-variable] int a,b; e ainda: Quote shiftr.c:26:13: error: expected declaration specifiers or ‘...’ before ‘&’ token shift_right(&alfabeto[0], 8, 1); ^ shiftr.c:26:27: error: expected declaration specifiers or ‘...’ before numeric constant shift_right(&alfabeto[0], 8, 1); ^ shiftr.c:26:30: error: expected declaration specifiers or ‘...’ before numeric constant shift_right(&alfabeto[0], 8, 1); PS: Não ligues às linhas que o meu compilador referencia porque são diferentes no teu cóigo. Eu não gosto dos {} como tu os usas e mudei-os para o meu gosto e por isso o número das linhas não conincide com as do teu code. Mas foi apenas isso que eu alterei, portanto os erros devem continuar a ser os mesmos. Edited December 1, 2019 at 07:11 PM by PsySc0rpi0n Kurt Cobain - Grunge misses you Nissan GT-R - beast killer Link to comment Share on other sites More sharing options...
Seabra Posted December 2, 2019 at 12:11 PM Author Report Share #616752 Posted December 2, 2019 at 12:11 PM Depois de muito partir pedra, cheguei a um resultado final ainda mais completo. O código permite deslocar para a esquerda ou para a direita um conjunto de bytes. o código que coloquei era um excerto do meu código total por isso haver alguma baralhação. de qualquer modo obrigado PsySc0rpi0n pelos teus comentários e tempo despendido #include <stdio.h> typedef unsigned char byte; // *ar - variable array //size - array lengt //Shift- number s shifts //input - Input bit left or right into byte //lr - Shift left=0 or Shift right=1 void shift_left_right(byte *ar, int size, int shift, byte input, byte lr) { while (shift-- > 0) { byte carry = 0; int k; if (!lr) { for (k = size; k-- > 0;) { if (k > 0 & !lr) { carry = (ar[k - 1] & 0x80) >> 7; } else { if (input) { carry = 0x01; } else { carry = 0x00; } } ar[k] = (ar[k] << 1) | carry; } } else { for (k = 0; k < size; k++) { if (input & (k=size-1)) { carry = 0x80; } else { carry = (ar[k + 1] & 0x01)<<7; } ar[k] = (ar[k] >> 1) | carry; } } } } void imprime(byte texto) { for (int i = 0; i < 8; i++) putchar('0' + !!((texto << i) & 0x80)); // putchar('\n'); } int main() { byte alfabeto[8] ={0b11111111, 0b00000011, 0b00000000, 0b00000000,0b00000000, 0b00000000, 0b00000000, 0b00000001}; shift_left_right(alfabeto, 8, 4,0,0); for (int i = 7; i>=0 ; i--) imprime(alfabeto[i]); return 0; } Link to comment Share on other sites More sharing options...
HappyHippyHippo Posted February 14, 2020 at 06:49 AM Report Share #617337 Posted February 14, 2020 at 06:49 AM esse código parece demasiado convoluto para fazer o que diz fazer, mas, como não vou me dar ao trabalho de o simplificar, e funciona como dizes, então parabéns IRC : sim, é algo que ainda existe >> #p@p Portugol Plus Link to comment Share on other sites More sharing options...
Recommended Posts
Create an account or sign in to comment
You need to be a member in order to leave a comment
Create an account
Sign up for a new account in our community. It's easy!
Register a new accountSign in
Already have an account? Sign in here.
Sign In Now