How to round off number to 0 or next 0.5

by Viper 12. June 2009 08:31

While working on a clock synchronization service, I had to dig up an old piece of code that I used to round off numbers. For this application I need to round the number to 0 or next 0.5 depending on it proximity to edge. For example if number is 8.001, it gets rounded off to 8.0 and if it is 8.6123 it will be rounded to 8.5. You can argue why not 9 instead of 8.5. Well the logic of the application demands that number is to be rounded down. Here is simple piece of code that will do the trick.


double roundDownNumber = Math.Round(actualNumber * 2.0, 0) / 2.0;

Just simple idea that multiply the number by 2 and round it to ZERO decimal places and then divide by 2.

Give your advice to big bosses and make money

Views: 1200

Tags: , ,

.Net | C#

Manipulating Bits and Bytes Using C#

by Viper 21. March 2009 05:45

Recently I was working on detection of MP3 file format when a user uploads a file to a web site. Part of the detection was to look for ID3 header and figuring out what flags were set in header. The format specification tells you at what bit locations to check for what flags. This meant that you will need some mechanism to figure out what bits are set with in a byte. If you are seasoned C/C++ developer, you pretty much know that this meant that doing some low level bit shift operations on that byte to rotate through them to check which bit is set and which is not set. So if you will write that code in C#, it will look like snippet below.


static void ConvertToBinaryFormat(byte n)
{
 var flags = new BitArray(8);
 var counter = 8;
 var idx = 0;
 while(counter > 0)
 {
  flags.Set(idx++, ((n & 1) == 1));
  n >>= 1;
  counter--;
 }
 PrintBitArray(flags);
}

static void PrintBitArray(BitArray ba)
{
 Console.WriteLine();
 for(int i = ba.Length-1; i >= 0; i--)
 {
 Console.Write("{0}", ba[i] ? 1 : 0);
 }
}

Looking at the code, you are already saying that I have already used BitArray class object in this function. Then why am I doing all the low level operations. I intentionally introduced BitArray here to demonstrate that you really do not need to do all the low level operations to check what bits are set. BitArray class already does that for you. Following one line of code does the same thing that I did by doing some bit shifting.


var newFlags = new BitArray(new byte[] {20});
PrintBitArray(newFlags);

If you look at PrintBitArray method, you will notice that it is using reverse loop to print each bit. When bit shit operation is being done, bit array gets filled from right to left. This means that higher order bits get pushed low. And BitArray class does the same thing. So you will have to access the array from bottom.

Give your advice to big bosses and make money

Views: 3863

Tags: ,

.Net | C#

Error CS0840: Automatically implemented properties must define both get and set accessors

by Viper 24. February 2009 06:38

Auto implemented properties are wonderful thins and save lot of coding and declaration in most of the situations. So a simple auto implemented property will look like as below.

public string Title
{get; set;}

No need to define a private variable to store the value. In some cases you want your property to be immutable meaning you don't want clients to be able to change the value and only allow private or internal access to the setter of this property. The specification of auto implemented properties says that you have to provide both get and set accessors. This does not mean that you have to provide public accessors for both. You can always provide private or inernal accessor for get.

public string Title
{get; internal set;}

Give your advice to big bosses and make money

Views: 851

Tags: ,

.Net | CSharp

Powered by BlogEngine.NET 1.5.1.7
Theme by Naveen Kohli

By Categories