5
Reply

Please explain the code on how does bit wise operator works..also Endianness

Suraj Rai

Suraj Rai

12 years ago
1.6k
Hi All,
I am trying to understand shift operator which is used in my project. What I came to know from google is that Bit shifting allows for compact storage of similar data as a single
integral value.
But how does that work. and what is the advantage. Can you explain me the part below program whats exactly happening in below lines. The code is taken from
http://www.codeproject.com/KB/graphics/dicomImageViewer.aspx (Tnx to Amarnath S)
int vr = (b0 << 8) + b1; and  case UT: ??
This programe reads a binary file, in medical terms a dicom file, which is basically a binary file.

Getting 4 bytes from a dycom file like this:
  byte b0 = GetByte();  // GetByte() is a private method that returns a single byte.
  byte b1 = GetByte();
  byte b2 = GetByte();
  byte b3 = GetByte();
Then doing this:
 int vr = (b0 << 8) + b1; // not sure what is happening here
Then a switch statement:
 switch(vr)
 {
 // Again didn get whats happening in case UT and QQ
 // where UT = 0x5554, is a constant declared above in the file 
 case UT: 
  // Explicit VR with 32-bit length if other two bytes are zero
  if ((b2 == 0) || (b3 == 0)) return GetInt();
  // Implicit VR with 32-bit length
  vr = IMPLICIT_VR;
  if (littleEndian)
  return ((b3 << 24) + (b2 << 16) + (b1 << 8) + b0);
  else
  return ((b0 << 24) + (b1 << 16) + (b2 << 8) + b3);
  // break; // Not necessary
 case QQ: // where QQ = 0x3F3F, is a constant declared above in the file
  // Explicit vr with 16-bit length
  if (littleEndian)
  return ((b3 << 8) + b2);
  else
  return ((b2 << 8) + b3);
 }
Can someone please give me some idea on this. What exactly might be happening in the lines having shift operators.
am little confused with bits and bytes..
hoping to get some reply.
Thanks a lot.


Answers (5)
Next Recommended Forum