Archive

Posts Tagged ‘beginner’

Nibbles and Bits–A discussion of Binary Operations in C#

January 8, 2012 Leave a comment

Before we start our discussion on Binary operations we should first discuss the different numerical systems. In general the most commonly used numbering systems (especially in programming) are Binary, Hexadecimal and Decimal.

Since all systems overlap some digits, in order to distinguish between the different numbering system we’ll use subscripts. For example 010 is 0 in decimal. 112 is binary 11 and 1016 hexadecimal.

The binary system is the easiest one to start with. There are 2 possible values for each digit 0 and 1. 02 = 010 , 112 = 310 . How does that work? It’s very simple. In the table below we have 8 positions, this is the same size as a byte. For each bit in the byte there is a binary value, meaning a 1 or 0. On the first row of the table it shows the value assigned to that bit. Because this system allow only two values (0,1) it’s called a Base2. In the table below you’ll notice that it’s value is an exponent of 2. This is very important and the basis of all numbering systems.

128 (27) 64 (26) 32 (25) 16 (24) 8 (23) 4 (22) 2 (21) 1 (20)
0 1 0 0 0 1 0 1

In order to convert a binary number to decimal simply multiply the values in the top row with the values in the bottom row. So the value of 010001012 = (1 * 64) + (4 * 1) + ( 1 * 1) = 6910. Surprisingly this is the same method used for all numbering systems. For example, in the table below is the same representation in decimal for the number 2012610:

100000 (105) 10000 (104) 1000 (103) 100 (102) 10 (101) 1 (100)
0 2 0 1 2 6

Lastly, using the same pattern we can show 12710 in hexadecimal.

256  (162) 16 (161) 1 (160)
7 F

As per the table 7F = (7 * 16) + (15 * 1) = 12710 .

That’s the quick recap on the concept of numbering systems. Now on to bit wise operations.

OR ( | ) and  AND ( & )

OR operations in C# are accomplished by using the | operator. The logic behind the OR operations is frequently used in if statements so all programmers are familiar with it, however, when writing out a bitwise OR it appears confusing simply because it’s in decimal notation.

 byte result = (17 | 5);
Console.WriteLine(result);
The output for the above operation will be 21. How do we apply a binary operator to byte values? You actually don’t, in the situation above the 17 is broken down to it’s binary equivalent and the 5 is broken down to it’s binary equivalent and each bit is “ORed”. Applying an OR operation to a binary  bit is easy to follow when you consider that a bit set to 0 is considered false and if a bit is set to 1 is considered true. With the OR operator if either side is set to true the result is always true. If both sides are false the result is always false. Below is a table that shows the example above’:
17 1 0 0 0 1
5 0 0 1 0 1
Result (21) 1 0 1 0 1
Once broken down to binary values, it demystifies the whole process of applying an OR operation to two bytes.
The AND operation when applied to bits only returns true (1) if both bits are true.
17 1 0 0 0 1
5 0 0 1 0 1
Result (1) 0 0 0 0 1

Putting it all together

What is the point of using these operations? There are quite a few operations that require and OR operation, however, the most common use is for storing Boolean values in a byte or integer value. Let’s say you need to keep track of 5 settings, New, Archive, Done, Hidden, Valid. You could create 5 Boolean variables to track them or just store them as bits in a byte. There are a lot of advantage to it, mostly because you can combine two sets of result just by conducting an OR operation. As demonstrated above you can combine the settings from two bytes easily into one.

To be Continued

Advertisements
Categories: Binary, C# Tags:

Localization and Globalization – Part 1 DateTime

December 27, 2011 Leave a comment

As the demand for applications that are “culture aware” becomes a much more frequent requirement, the implementation of it becomes more and more important. Globalization is more then just implementing the ability to change the language of the user interface, it’s many more details. It’s the way digit groupings are done, money displayed and date format to name a few.

Today I decided to dedicate this article to the various considerations that revolve around the DateTime class.

A few resources to have a look at if you’re not familiar with DateTime Class.

Resources

MSDN Library Entry for DateTime Struct

MSDN Entry for DateTime Picker Class

Code Sample

The Basics

DateTime is not a class, it is a struct. The challenge that most programmers have with this is what to initialize the struct as. For example, when initializing an int for the most part it’s initialized to 0 or –1. This is (of course) generally speaking, for DateTime I would recommend that when declared it should be initialized to DateTime.Min. DateTime.Min is equal to 00:00:00.0000000, January 1, 0001 which is a highly improbable date to ever use within a program.

The other consideration is current time. There are two methods for that DateTime.Now and DateTime.UtcNow. I would strongly recommend that UtcNow is used as often as possible. If the current DateTime is used frequently in a high speed environment (such as time stamps for packet sniffing) you must use DateTime.UtcNow. In the example program (see under resources) the differences between UtcNow and Now is below copied from the output of the program:

For 100000 iteration it took a total of 241 ms when Using DateTime.Now and 144 ms total when using DateTime.UtcNow

As you can see UtcNow is significantly faster then Now.

One last word of caution that most of the rest of the blog will be about, whenever possible (I’ve made it an unbreakable rule for me and my team) do not store dates and/or times as anything other then a DateTime (especially a string) and always verify and validate user input.

 

Verifying Validating Dates

The best way to make sure that the date that was entered is the right one is to restrict the user to a DateTime Picker. However, should that not be an options there’s a need to create a system of checks that both provide hints for the user on how to enter the date and once entered to verify that the date entered is the same as what the user intended. For example if the user enters 1/2/2011 did they mean January 2nd or February 1st? We’re going to take a detour and discuss localizing the date format first and then put the two topics together and create a user control that will cover both topics in one example.

 

Localizing DateTime Display

The screen shot below is intended to demonstrate the differenced between language (Culture Settings) and DateTime format. The screenshot is taken from the Sample that can be found here.

The first column is the description of the format. In the screen shot  I chose el-GR (Greek) as the culture. It’s important to note that if the only the format is applied it changes the date display (second column) to match what is the norm in Greece, however it doesn’t change the language (December is still shown as the month). However, on the third column where I apply the culture it changes both the date format and the language of the date.

DateFormats

Bringing it All Together

To bring it all together there’s a user control that provides a user a textbox to enter the date. When the control is loaded it changes the {0} in the “Enter Date” label such that it shows the format that is excepted for the current culture. Furthermore once the date is entered the the text box loses focus (meaning the user moved to the next field needing their input)the date is parsed and shown written out below the text box, that way the user can confirm it’s the right date.

DateScreenShot

Happy Codin’