c - How to get and set bits for bits in char string? -


Normally small operations are in small data width such as int, unsigned int or wchar_t, assuming we have a small format Want to use bit strings, how to change, receive and set bits for bits in four strings?

given

  #define  

NumberOfState 2000 / number of bits # defined numberofbits in these 1 bit 8 characters * Records; Int number off-sea-rearrentprintsbits = seat ((float) number offset / (float) number offbits in 1 bat)); Record = (four *) mauloq (sizeoff (four) * numberofcharacter aeratorprintsbits); // record = "NAXHDKAN552ajdasdadNDfadsEBEAfA8gda5214S"; // Optional: By setting the file set to // according to input initialization. After this, make free (record);

How we can operate a little bit, such as

  i change * ii * get bits from a specific bit position in the record iii. * Set bits from the specific bit position in Record  

Please try an following with the following code Do:

  #include & lt; Stdio.h & gt; #include & lt; String.h & gt; # Include & lt; Stdlib.h & gt; Int isLittleEndian = 1; Check zero Indian (zero) {union {cum inum; Four c [size (small)]; } United Nations; Un.inum = 0x0102; If (Un. [0] == 1 and amp; un.c [1] == 2) {printf ("big_endian. \"); IsLittleEndian = 0; } And if (un.c [0] == 2 & amp; amp; un.c [1] == 1) {printf ("little_endian. \"); IsLittleEndian = 1; }} Zero shift_L (char * src, char * dst, int len, int n) {int shiftBytes = n / 8; Int Shift Beats = N% 8; Memeset (DST, 0, Lane); Memcpy (dst, src + shiftBytes, lane - shiftBytes); If (shiftBits) {int i = 0; Unsigned less tmp = 0; For (i = 0; i  = 0; i--) {if (Little British) {tmp = * (dst + i-1) & lt; & Lt; 8 | * (DST + i); TMP & gt; & Gt; = ShiftBits; * (DST + I) = * ((* four *) and TMP); } Other {TMP = * (Small *) (DST + I-1); TMP & gt; & Gt; = ShiftBits; * (DST + I) = * ((* four *) and TMP + 1); }}}} Int getBit (char * src, int n) {unsigned char tmp = * (src + n / 8); Unsigned four mask = (0x1 <- (8 - n% 8 - 1)); Int bit = 0; Bit = (tmp and mask) & gt; 0; Printf ("% d", bit); } Zero setbit (char * src, int n, int bit) {unsigned char * pTmp = src + n / 8; Unsigned four mask = (0x1 <- (8 - n% 8 - 1)); If (bit) {* pTmp | = Mask; } And {* PTMP & amp; = ~ Mask; }} Zero dump (unsigned char * source, int lane) {int i = 0; Int j = 0; Unsigned four mask = 0; For (i = 0; i & lt; len; i ++) {for (j = 0; j & lt; 8; j ++) {mask = 0x1 & lt; & Lt; 8 - J-1 print ("% d", (* (src + i) and mask)> 0); }}} Zero main () {char * record = "NAXHDKAN552ajdasdadNDfadsEBEAfA8gda5214S"; // char * record = "NAXHDKA"; Int recordLen = strlen (record); Char * buffer = null; Int i = 0; CheckEndian (); Record LAN = record LAN + record LAN% 2; Buffer = mallow (record lan); Memcpy (buffer, record, record LAN); Printf ("\ nInput Bit Stream: \ n"); Dump bin (buffer, record LAN); Printf ("stream from \ nbits beet: \ n"); For (i = 0; i & lt; record LAN * 8; i ++) {getBit (buffer, i); } Setbit (buffer, 8, 1); Setbit (buffer, 9, 0); Setbit (buffer, 10, 1); Setbit (buffer, 11, 1); Printf ("After set bit \ nbit stream: \ n"); Dump bin (buffer, record LAN); Shift_L (record, buffer, record LAN, 1); Printf ("\ \ bit stream after shift_L: \ n"); Dump bin (buffer, record LAN); Shift_R (record, buffer, record LAN, 9); Printf ("\ n bit stream after shift_R: \ n"); Dump bin (buffer, record LAN); Printf ("\ n"); Free (buffer); }  

Comments