How to use bitshifting in Java
I am trying to construct an IP header. An IP header has the following fields: Version, IHL, DSCP etc. I would like to populate a Byte Array such that I can store the information in bytes. Where I get confused however is that the Version field is only 4 bits wide. IHL is also only 4 bits wide. How do I fit the values of both of those fields to be represented as a byte? Do I need to do bitshifting? E.g. Version = 4, IHL = 5. I would need to create a byte that would equal 0100 0101 = 45h or 69 decimal. (byte) (4 << 4) | 5 This shifts the value 4 to the left, then sets lower 4 bits to the value 5.