0

I'm developing app that will send data over RTP, but I almost never worked with bytes. Right now I'm trying to use BitArray:

byte[] toSend = new byte[12];
BitArray b = new BitArray(new int[] { 2 });
b.CopyTo(toSend, 0);

but it works with Int32, so 2 is represented like 0100..0, and that's not what I need. I have 2 questions here:

  1. How should i combine 2|1|1|4 bits into one byte? I think there should be something that looks like:

    int version = 2;//2 bits
    int padding = 0;//1 bit
    int extension = 0;//1 bit
    int ccrc = 0;//4 bits
    
    byte[] toSend = new byte[1]{version+padding+extension+ccrc};
    
  2. For some headers there are 16 bits reserved, so i need something like this: 0000000000000000(16), but i don't know how to create this kind of variable, and how to write 16 bits into two bytes.

Igor
  • 60,821
  • 10
  • 100
  • 175
Jamil
  • 830
  • 2
  • 15
  • 34
  • 1
    Dont forget to check for endianness/ add a exception if the system has the wrong endianess! – Git Feb 21 '17 at 14:11

2 Answers2

2

If I understand correctly, you are trying to create a byte from bits.

Why not use a a function that takes a byte, the bit value and the position to place the bit as explained here?

Community
  • 1
  • 1
Git
  • 214
  • 5
  • 16
  • I think it answers my first question. But how can i store 16bit and 32bit values? Simple Int16 or Int32? – Jamil Feb 21 '17 at 13:54
  • you can either create a byte array ( that way it wouldnt matter the int size) or let the function return a long long and cast the result to what ever you need – Git Feb 21 '17 at 13:55
2

Starting from the second point. How to create a variable that holds up 16 bits ( 2 bytes ) containing only 0's :

char _16bitsOfZero = '\0'; // this will have the default value of NULL character
// which basically is 0000 0000 0000 0000 

Going further to creating one byte value from your 4 integers :

int version = 2; // 2 bits which will be shifted to the most left
int padding = 0; // 1 bit which will be shifted to the most left but just before version
int extension = 0; // 1 bit which will be shifted to right before padding
int ccrc = 0; // 4 bits that ... wont be shifted.

// first of all this all is going to be one byte ( 8 bits )
// 00 0 0 0000 <- beginning value
byte b = 0;
// now we want to shift our ccrc to be in the back
// 00 0 0 ccrc <- like this
// to put this value there we need to shift this 
b = (byte)ccrc;
// now we want to shift extension right after ccrc value
// remembering that ccrc is 4 bits wide :
b |= (extension << 4);
// now do the same with padding :
b |= (padding << 5); // 4 because previous value was 1 bit wide
// now the same with version :
b |= (version << 6);
// output from this should be :
// 10 0 0 0000
// which is 128

To check if it really is 128 go here

// to be more precise :
// assume ccrc = 15 ( 0000 1111 )
// and b = 0 ( 0000 0000 )
// now use shift :
b |=
    (ccrc << 1) // will produce the output of ( 0001 1110 )
mrogal.ski
  • 5,828
  • 1
  • 21
  • 30