YuviSense: Codin Kid

Yuvi, a 17 year old wannabe geek from India.
  • rss
  • Blog
  • Photography
  • Indians on Twitter
  • About Me
  • Contact

[Tech]Trying to save Bytes may Byte you back!

June 25, 2006 | 7:39 pm

Update: Just clarified a bit of stuff towards the end after a reread.

This tech post is long overdue, and has spent quite a lot of time in my drafts folder. I guess it’s atlast time for it to see the light of day, so I’m posting it…

Lemme start off with code samples right away.

What’s the output of this code snippet ?

Dim i as Int64 = 200
Console.WriteLine ( i.GetType.ToString )
Console.WriteLine ( (i-10).GetType.ToString )

Compile the code in your mind, and you’ll find a lot of Syntax errors if you’re a C# guy, and the following output if you’re a VB guy:

System.Int64
System.Int64

Pooh. Nothing unexpected. But, wait….What’s the output of this code snippet ?

Dim b as Byte = 200
Console.WriteLine( b.GetType.ToString )
Console.WriteLine ( ( b -10).GetType.ToString)

So, just like in the last example, you should get System.Byte as the output both times, right ? Wah! I told you to wait, right ?

System.Byte
System.Int32

So, where did that System.Int32 come from? If an operation on an Int64 results in an Int64, then why is an operation on a Byte resulting in a System.Int32? Were the consipary theorists right, after all?

Oh wait! Lemme add one more to the mix!

What’s the output of this ?

Dim s As Short = 200
Console.WriteLine( s.GetType.ToString )
Console.WriteLine( (s - 10).GetType.ToString )

If you’ve been sleeping all along, you would have expected two System.Int16s, but ofcourse you weren’t sleeping, so you’ll see:
System.Int16
System.Int32

Uh-oh. So, what the hell is actually going on? So, to find out this, and some other things, I was digging through ECMA-355, the .NET ECMA Specs.

Aha! And, I fount out. The aptly named section 1.1.1 of the document says:

The CLI only operates on the numeric types int32 (4-byte signed integers), int64 (8-byte signed
integers), native int (native-size integers), and F (native-size floating-point numbers). However,
the CIL instruction set allows additional data types to be implemented:

Uh oh. So, what that means is, that the .NET CLI only knows about 32 Bit and 64 Bit Ints. When they say Native Int, they mean that if you implement .NET for a, say 16 bit Machine, then the CLI will “get” 16 bit Integers…

But wait, you say. The CLI already has 16 bit integers. Heck, it even has 8 bit integers!

But, ah I love to prove people wrong, and that’s why I spent the time to read that headsy Technical Document. In the same old Section 1.1.1, it says:

Convert instructions that yield short integer values actually leave an int32 (32-bit) value on the stack, but it
is guaranteed that only the low bits have meaning (i.e., the more significant bits are all zero for the unsigned
conversions or a sign extension for the signed conversions).

Well, basically, what this means is, even if you use a Byte datatype[8 bits], you are still using 32 bits. Only that the 24 extra bits are Zero, always. They just lie there, useless, taking up precious memory space which could be used for some more 1s. Same for 16 bit ints: It still uses 32 bits, but only that 16 of those are not used and left zero.

When you see Short and Byte types in the Framework, they are just a mere illussion. They are still using 32 bits, just pretending that they’re using less. That’s why, the operations return the true type, rather than the illussionary one.

Update:Just thought that I’ll add that the operation returns the true type, because the values are held on the Evaluation Stack while the operation[in this case addition] is being performed. Atleast, that’s the reason I think. The normal GetType on the type gives you the illussionary type, because it’s on the heap, not on the Evaluation Stack, which is real Harichandra v2.0.

Moral of the Story: Don’t use Shorts and Bytes, if your sole reason is thinking that you’re saving space: You are not….

Now, the best appreciation you could give to this small depressed kid living in a lone world surrounded by non-geeks with not many to talk to is to leave a comment here…

Technorati Tags: Byte, Bit, Wasted Bits, Wasted Memory, .NET, C#, VB.NET, Writing, Saving Space

Categories
Tech
Comments rss
Comments rss
Trackback
Trackback

« School Wanna see a Phishing site ? »

2 responses

Really nice post da :D and the improvement is clear

Aswin Anand T.H. | June 25, 2006 | 11:06 pm

Really nice post da :D and the improvement is clear :-), especially the last line ;-)

reading a real good Technical post after a long time

Rajagopal | June 26, 2006 | 7:33 pm

reading a real good Technical post after a long time da. Keep it going!

Leave a comment

You can use these tags : <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>

Contact Me

Email: yuvipanda@gmail.com
IM: yuvipanda@msn.com
GTalk: yuvipanda

My Photos


View my Flickr Page

My Badge


IndiBlogger - Where Indian Blogs Meet

Archives

  • November 2008
  • October 2008
  • September 2008
  • August 2008
  • July 2008
  • June 2008
  • May 2008
  • April 2008
  • March 2008
  • December 2007
  • November 2007
  • October 2007
  • September 2007
  • August 2007
  • July 2007
  • June 2007
  • May 2007
  • April 2007
  • March 2007
  • February 2007
  • January 2007
  • December 2006
  • November 2006
  • October 2006
  • September 2006
  • August 2006
  • July 2006
  • June 2006
  • May 2006
  • April 2006
  • March 2006
  • February 2006
  • January 2006
  • December 2005
  • November 2005
  • October 2005
  • September 2005
  • August 2005
  • July 2005
  • June 2005
  • May 2005
rss Comments rss valid xhtml 1.1 design by jide powered by Wordpress get firefox