1
votes

I have trouble with figuring out how to put 9 char into an array of 4 unsigned short in c programming.

I know that a char is 1 byte, but only 7 bits are used because ascii table are 0 ~ 127, so I need 7 * 9 = 63 bits. since short is 2 bytes each, then it has 16 bits for each short. array of 4 short is 4 * 16 = 64 bits. Which means that I can fit those 9 char into an array of 4 unsigned short

so basically I have

unsigned short *ptr, theArray[4], letter = 0;

int mask;

//read 9 char and save it to the array

What I don't understand is how to read the 4 characters input and save it to theArray. The limitation is that I can't put them into a string first, I can't declare anything else except int. I know I have to do some bit manipulation, but I just don't know how to read the input. Thank you for your help!

2
Now 4 or 2 unsigned shorts? Also, you can't make assumptions about the size of char in bits and that of short in bytes. Finally, what have you tried? - user529758
Where do you want to get the input from? A file? Standard input? - Carl Norum
It might be easier to use a int64_t instead of an array of 4 shorts - kbickar
Hi, sorry I'm new to this site. 4 unsigned shorts, the title is wrong. The input is standard input, my task is to read input such as "test 12" and produce an output "21 tset" using just that short *ptr, theArray[4], letter, and maximum of 3 ints, no char or string allowed. - DumbProgrammer
why don't edit the title? And 4 shorts is typically 64 bits which fits 63 bits perfectly - phuclv

2 Answers

0
votes

This is where the shift and or operators come into play.

I can't give any exact examples but you can use them together to "smash" the chars into an array of unsigned shorts.

A quick example, possibly not exactly what you're looking for, would be:

char j = 'J';
char y = 'Y';
unsigned short s = ( y << 7 ) | j;
0
votes

If we can assume that unsigned short = 16 bits and char = 8 then unless I have made a typo what about this:

#include <stdio.h>

int main()
{
  unsigned short *ptr, theArray[4], letter = 0;
  int mask;

  // theArray:
  //  |<-------0------>|<-------1------>|<-------2------>|<-------3------>|
  // characters:
  //   0111111122222223 3333334444444555 5555666666677777 7788888889999999

  // Because we use |= first clear the whole thing
  theArray[0] = theArray[1] = theArray[2] = theArray[3] = 0;

  /* char 1 */  letter=getchar();
                theArray[0] |= 0x7F00 & (letter << 8);

  /* char 2 */  letter=getchar();
                theArray[0] |= 0x00FE & (letter << 1);

  /* char 3 */  letter=getchar();
                theArray[0] |= 0x0001 & (letter >> 6);
                theArray[1] |= 0xFC00 & (letter << 10);

  /* char 4 */  letter=getchar();
                theArray[1] |= 0x03F8 & (letter << 3);

  /* char 5 */  letter=getchar();
                theArray[1] |= 0x0007 & (letter >> 4);
                theArray[2] |= 0xF000 & (letter << 12);

  /* char 6 */  letter=getchar();
                theArray[2] |= 0x0FE0 & (letter << 5);

  /* char 7 */  letter=getchar();
                theArray[2] |= 0x001F & (letter >> 2);
                theArray[3] |= 0xC000 & (letter << 14);

  /* char 8 */  letter=getchar();
                theArray[3] |= 0x3F80 & (letter << 7);

  /* char 9 */  letter=getchar();
                theArray[3] |= 0x007F & letter;

  return 0;
}