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.
Loading
Suraj RaiPosted Nov 28, 2011, 1:56 AM
VulpesPosted Nov 25, 2011, 6:01 AM
Karthik AgarwalPosted Nov 25, 2011, 5:15 AM
With the example you stated everything is correct except you are multiplying with the same number 5 do the multiplication with 2 instead for 4 number of times.
i.e
int vr = 5;
int result = vr << 4;
int result = vr * 2 * 2 *2 *2; i.e 5 * 2 * 2 * 2 * 2;//80 is the result.
same way do it with 65535 * 2 * 2 * 2 ........ so on 8 times which gives 16776960.
in the same way << 24 , << 16 , so on means multiplying the original number with 2 those many number of times like 24 or 16 or 8 etc etc... and then your code is adding with b0 or b1 or b2 some thing like that.
Suraj RaiPosted Nov 25, 2011, 5:06 AM
Thanks a lot for the reply.
but i am not clear of few things.
what do you mean by 8 times multiply..do you mean..say i have
int vr = 5;
int result = vr << 4;
is it mean : 5 * 5 * 5 * 5 ??
because when i tried to multiply ur example e.i 65535, it does'n give 16776960.
sorry for asking you again, i guess i am getting it wrong.
Please explain a bit more as I am new to computing lang.
And can you tell me about little endian ?
I mean why, if little endian, they are doing leftshift byte wise 24, 16,8 ..
if (littleEndian)
return ((b3 << 24) + (b2 << 16) + (b1 << 8) + b0);
Thanks.
Regards
Karthik AgarwalPosted Nov 25, 2011, 4:04 AM
for example if you consider binary data 11111111 which is nothing but 0xFFFF in hexadecimal value. but either you take binary, decimal or hexadecimal the operation is one and the same it multiplies the number with 2 that's it.
int vr = 11111111 or 0xFFFF or 65535;
int result = vr << 8;//result will have 16776960
the variable result will be holding a value 16776960 which is like multiplying the number (i.e 11111111 or 0xFFFF or 65535) eight number of times. That is what is done in the above code.
In the similar way there is right shift as well >> which does the inverse operation of left shift i.e it does division those many number of times based on the index beside that.
ex: int vr = 16776960;
int result = vr >> 16776960;// result will have 65535
For any further doubts feel free to post back.