Starting with the address A in the memory of the MK there are N records with the length of k bits each. Records are aligned to the word boundary. Write functions for reading / writing individual bits for any record from this set.

1-word = 4-bytes = 32 bits The question is how to make a record length, for example, not standard 32, but let's say 33 bits or 37 bits? and what operations to use for this?

That's what happened with me, but this is not entirely correct:

#include <stdio.h> #include <stdlib.h> #include <string.h> #include <cmath> //extern char tim(char s9[100], int OldSize, int NewSize); extern void _WRITE_MASK1(); extern void _WRITE_MASK2(); extern void _WRITE_BIT(long *j, int num_z, int num_byte, int num_bite, int _vol); extern int _READ_BIT(long *j, int num_z, int num_byte, int num_bite); #define N 1 // Π’Π²ΠΎΠ΄ ΠΊΠΎΠ»-Π²Π° записСй ΠΏΠΎ 4 Π±Π°ΠΉΡ‚Π°. int main() { int K=32; // Π’Π²ΠΎΠ΄ ΠΊΠΎΠ»-Π²Π° Π±ΠΈΡ‚ΠΎΠ² Π² ΠΊΠ°ΠΆΠ΄ΠΎΠΉ записи( ΠΎΡ‚ 1 Π΄ΠΎ 32 ) int Z=K-1; if((K<1)||(K>32)) { return 0; } unsigned long mass [N]; for (int i=0; i<N; i++) // Ρ„ΠΎΡ€ΠΌΠΈΡ€ΠΎΠ²Π°Π½ΠΈΠ΅ Π±ΠΈΡ‚ΠΎΠ² Π΄Π»ΠΈΠ½ΠΎΠΉ К. { if(Z<2) mass[i]=pow(2,Z); if ((Z>=3)&(Z<10)) mass[i]=pow(2,Z)+pow(2,Z-1)+pow(2,Z-2)+1; if ((Z>=10)&(Z<20)) mass[i]=pow(2,Z)+pow(2,Z-2)+pow(2,Z-4)+pow(2,Z-6)+pow(2,Z-8)+i; if ((Z>=20)&(Z<32)) mass[i]=pow(2,Z)+pow(2,Z-4)+pow(2,Z-8)+pow(2,Z-12)+pow(2,Z-16)+i; } _WRITE_MASK1(); _WRITE_MASK2(); int rtf =1; // 0 ΠΈΠ»ΠΈ 1 int bit=1; // ΠΎΡ‚ 0 Π΄ΠΎ 7 int byte=3; ; // ΠΎΡ‚ 0 Π΄ΠΎ 3 int zap=4; // ΠΎΡ‚ 0 Π΄ΠΎ ΠΊΠΎΠ»-Π²Π° записСй -1 int k=(sizeof(mass))/sizeof(mass[0]); // ΠΎΠΏΡ€Π΅Π΄Π΅Π»Π΅Π½ΠΈΠ΅ ΠΊΠΎΠ»-Π²Π° элСмСнтов if(((bit>7)||(bit<0)||(byte>3)||(byte<0)||(zap<0)||(zap>k-1))||((rtf!=0)&(rtf!=1))) { return 0; } _WRITE_BIT(&mass[0],zap,byte,bit,rtf); //zap ΠΎΡ‚ 0 Π΄ΠΎ Ρ€Π°Π·ΠΌΠ΅Ρ€Π° массива-1; byte ΠΎΡ‚ 0 Π΄ΠΎ 4, Π±ΠΈΡ‚ ΠΎΡ‚ 0 Π΄ΠΎ 7. УстанавливаСм Π±ΠΈΡ‚ Π² памяти Ρ€Π°Π²Π½Ρ‹ΠΉ rtf int bitRead=1; // ΠΎΡ‚ 0 Π΄ΠΎ 7 int byteRead=3; // ΠΎΡ‚ 0 Π΄ΠΎ 3 int zapRead=4; // ΠΎΡ‚ 0 Π΄ΠΎ ΠΊΠΎΠ»-Π²Π° записСй -1 if((bitRead>7)||(bitRead<0)||(byteRead>3)||(byteRead<0)||(za pRead<0)||(zapRead>k-1)) { return 0; } int b = _READ_BIT(&mass[0],zapRead,byteRead,bitRead); int c=10; } 
  • @Twixs, To format a code, select it with the mouse and click on the {} button of the editor. - Bars
  • @Twixs, Please rephrase your question: correct grammatical errors, place commas correctly. If you add more clarifying information, you will speed up the receipt of the answer. @Twixs, Please arrange the questions according to the rules of the community, otherwise they will be deleted. - Nicolas Chabanovsky ♦

1 answer 1

You need to "finish off" the end of the record to the length of the word. In your case (C-style), the easiest way is to work with structures where you can define alignment in advance. Suppose there is such data:

 typedef struct tagRecord { float data1; // word (4 Π±Π°ΠΉΡ‚Π°) double data2; // double word (8 Π±Π°ΠΉΡ‚) int data3; // word (4 Π±Π°ΠΉΡ‚Π°) unsigned char data4; // 1 Π±Π°ΠΉΡ‚ } Record; // sizeof(Record) == 17, //Π½ΡƒΠΆΠ½ΠΎ "Π΄ΠΎΠ±ΠΈΡ‚ΡŒ" Ρ€Π°Π·ΠΌΠ΅Ρ€ Π΄ΠΎ кратности Ρ€Π°Π·ΠΌΠ΅Ρ€Ρƒ слова (4), Ρ‚.Π΅. Π΄ΠΎ 20. // НапишСм ΠΎΠ±Π΅Ρ€Ρ‚ΠΊΡƒ с Π½Π΅ΠΎΠ±Ρ…ΠΎΠ΄ΠΈΠΌΡ‹ΠΌ Π²Ρ‹Ρ€Π°Π²Π½ΠΈΠ²Π°Π½ΠΈΠ΅ΠΌ typedef struct tagAlignedWrapper { Record data; char align[3]; } AlignedWrapper; 

Extreme 3 bytes can be simply ignored (not filled in and not processed in functions), but they will automatically align our structure.

This is useful for mixed data. In your case, 33 bits will be represented as:

 typedef struct tagRecord { char data[5]; // 7 Π±ΠΈΡ‚ Π² ΠΊΠΎΠ½Ρ†Π΅ ΠΈΠ³Π½ΠΎΡ€ΠΈΡ€ΡƒΠ΅ΠΌ char align[3]; } Record_33bit_aligned; 

And 37 bits , oddly enough, in the same way =)

 typedef struct tagRecord { char data[5]; // 3 Π±ΠΈΡ‚Π° Π² ΠΊΠΎΠ½Ρ†Π΅ ΠΈΠ³Π½ΠΎΡ€ΠΈΡ€ΡƒΠ΅ΠΌ char align[3]; } Record_37bit_aligned;