Hi,
Shifting is used when you need information at bitlevel
Consider the number 68437143. In the file it would be represented as
00000100 00010100 01000100 10010111
Hidden inside is the dimensions of an image where the information is
arranged as
0000 010000 01010001000 10010010111
Unused Colors Width Height
4 6 11 11
To be able to get this information we need to do bit manipulation
By filtering the number with 2047 we get the lower 11 bits
000001000001010 001000100100101 11 (68437143)
&
000000000000000 000000111111111 11 (2047)
=
000000000000000 000000100100101 11 (1175)
[code]
int n = 68437143;
int filter = 2047
int Height = n & filter; // 1175
To get Width we shift the bits 11 places to the right
000001000001010 001000100100101 11 (68437143)
>11
000000000000000 010000010100010 00 (33416) 10010010111 has been pushed out
[code]
n = n >11;
Do the same filtering to get the Width
000000000000000 010000010100010 00 (33416)
&
000000000000000 000000111111111 11 (2047)
=
000000000000000 000000010100010 00 (648)
Shift right 11 more places
000000000000000 010000010100010 00 (33416)
>11
000000000000000 000000000000100 00 (16)
Summarized in code it will look like
int number = 68437143;
int filter = 2047;
int height = number & filter;
number = number >11; // you could also write number >>= 11
int width = number & filter;
int color = number >11;
Image size = 648x1175x16
For instance, image files pack values int bits, not bytes.
On Fri, 24 Nov 2006 10:44:45 +0100, <ga********@myw ay.comwrote:
What does shift do? I read this thread and was intrigued but don't
understand it's use.
I am just learning the basics.
Please advise.
Morten Wennevik wrote:
>On Fri, 24 Nov 2006 10:14:50 +0100, Jay <nospamwrote:
So is that a bug?
It's by design. When you think about it, shifting a 32 bit integer with
anything more than 31 places doesn't make any sense.
http://msdn2.microsoft .com/en-us/library/xt18et0d.aspx
--
Happy Coding!
Morten Wennevik [C# MVP]
--
Happy Coding!
Morten Wennevik [C# MVP]