Friday, August 15, 2014

Internationalization the Simple Way



These days, if you are shipping an application or building a web application for an international audience, one of many questions revolves around how to make the application render content for the correct locale. This has often seemed like a daunting task for many developers. The truth is, internationalization is not that difficult in most modern web frameworks. If you are developing a native application, this has often been a bit more challenging, but after reading this blog post there should be no more excuses.

There are several approaches that have been set forward over the years-- many of which are burdensome or overly complex. I base my strategy upon a very simple fact. In most environments, a locale is attached to the current thread so you can pull the locale during run-time. A few years ago, I encountered the Spring framework which provides a LocalizableMessageSource that makes localization intuitive and automatic. However, for those of you in the .NET world, there isn't anything quite so powerful as Spring. I stole this idea from the guys at Spring. It goes like this:

Load a hash table of keys to message values for each locale. Then, load another hash table of locales to message tables. When you want to retrieve a message to display, look up the correct hash table for the current thread's locale. Next, retrieve the correct message from the returned hash table. That's it! Now the only challenge is providing a useful mechanism for persisting and loading initial hash tables. My approach was to create message files for each locale. Each message file contains key value pairs where the same keys are in each file, and the values are the localized messages for each locale. At start up, I simply load and parse the message files and build my data structure.

So that you don't have to do this manually, I have open sourced my solution: LocalizableMessageStorage.
 Here is the code on github.

Here is how to use it assuming an MVC architecture:

public class LoginController
{
  private LocalizableMessageStorage _localizableMessageStorage;
  public LoginController(LocalizableMessageStorage localizableMessageStorage)
  {
    _localizableMessageStorage = localizableMessageStorage;
  }
 
  public LoginModel InitializeModel(bool isFromPasswordReset)
  {
    return new LoginModel()
    {
      LoginButtonLabel = _localizableMessageStorage["lp_loginBtnLabel"],
      LoginButtonReset = _localizableMessageStorage["lp_loginBtnReset"],
      PageTitle = _localizableMessageStorage["lp_pageTitle"],
      UserNameLabel = _localizableMessageStorage["lp_userNameLabel"],
      UserPasswordLabel = _localizableMessageStorage["lp_userPasswordLabel"],
      UserNameValidationMessage = _localizableMessageStorage["lp_userNameValidationMessage"],
      PasswordValidationMessage = _localizableMessageStorage["lp_passwordValidationMessage"],
      ForgotPasswordText = _localizableMessageStorage["lp_forgotPasswordText"],
      ResetPasswordInstructionsText = _localizableMessageStorage["lp_resetPasswordInstructionsText"],
      UserMessage = _localizableMessageStorage["lp_userMessage"]
    };
  }
}

Now your model is filled with internationalized strings. This can be done for any style .NET application--Windows Forms, WPF, ASP.NET, ASP.NET MVC, and so on...-- as long as you use a sane architecture. I have used it successfully in both MVC and MVP architectures. It is thread safe, so it is okay to have the same instance of this in multiple controllers.

Here is an example of how an instance of LocalizableMessageStorage can be instantiated:

string localizationDirectory = HttpContext.Current.Server.MapPath("~/App_Data/Localization");
const string localizationPattern = @"messages([\w-]+)localization\.properties";
 
//default culture is EN-US. If nothing is found, we still want to return english.
CultureInfo fallbackLocale = CultureInfo.CreateSpecificCulture("EN-US");
LocalizableMessageStorage messages = new LocalizableMessageStorage(localizationDirectorylocalizationPatternfallbackLocale);

Now it is ready to use.  Obviously, this is in the context of an ASP.NET application, but you can use it in any context you choose.

Notice the localizationPattern value. It is a regex that tells the system how to parse your message files. It will look in the specified directory for files that match that pattern. Then it will use the group to pull the locale from the file name. There must be one match group that returns the canonical locale name for this to work.

Finally, here is my sample message file:

# ~/App_Data/Localization/messageEN-USlocalization.properties
# EN-US locale file for website.
 
# Login Page
lp_page_title = My Awesome Website - Login
lp_username = Email Address:
lp_password = Password:
lp_login_btn = Log In
lp_login_reset = Reset
lp_validation_username = Email Address is required.
lp_validation_password = Password is required.
lp_login_failure = Invalid Email Address/Password.
lp_forgot_password = Forgot Password?
lp_reset_password_directions = Are you sure you want to reset your password? An email will be sent to you with instructions on how to recover your password.
lp_email_sent= Email Sent.

I then have files exactly like this for each locale with the name for each locale in the file name, the same keys in each file, and the values as the localized messages.

That's all there is to it. So, internationalization is easy; don't be intimidated, and quit making English only websites!!!

Help yourself to the code. If you have improvements, email me and I will get you write access to the repository.

Friday, September 20, 2013

MVC with ASP.NET Web Forms

For those of us who have been doing development for a little while, we are used to the coming and going of fads. Especially so, we are used to Microsoft putting age old design patterns on things and marketing them as the next big thing. These days I am always being asked by .NET developers: "Do you know ASP.NET MVC?" I always respond, "I have always used MVC in my ASP.NET applications so yes." Seriously, what is the big deal? Wasn't the point of ASP.NET Web Forms to streamline the development process? Wasn't it replacing existing MVC frameworks of its day? Now all of the sudden, everyone is screaming about how slick ASP.NET MVC is despite the fact that some of the most useful features of ASP.NET Web Forms, like data bound controls, wired event handling etc... aren't even available.

So just to clear the air, MVC has always been easy with traditional ASP.NET.  There is no need to abandon something that works really well just so Microsoft can tell you what you can and can't do. Don't buy into the hype. If you are into ASP.NET MVC, by all means keep using it. But, if you have been using Web Forms for a while and you like it, don't let some architecture astronaut sell you some bill of goods about how Microsoft is bringing something new to the game when they are just forcing you to practice good design to begin with by removing flexibility from your arsenal.

So without further adieu, here is one of the many ways that you can use the MVC pattern in classic ASP.NET using the example of a simple Web Forms login page:

First, create your model:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading;
using System.Web;
using [MyStuff].Common.Utils.Localization;
 
namespace [MyStuff].Website.Login
{
  public class LoginModel
  {
    LocalizableMessageStorage messages;
 
    public LoginModel(LocalizableMessageStorage messageStore)
    {
      messages = messageStore;
    }
 
    public string PageTitle
    {
      get { return messages.GetLocalizedMessage(Thread.CurrentThread.CurrentUICulture, 
                                                  "lp_login_message"); }
    }
 
    public string UserLabel
    {
      get { return messages.GetLocalizedMessage(Thread.CurrentThread.CurrentUICulture, 
                                                  "lp_username_message"); }
    }
 
    public string PasswordLabel
    {
      get { return messages.GetLocalizedMessage(Thread.CurrentThread.CurrentUICulture, 
                                                  "lp_password_message"); }
    }
 
    public string UserName
    {
      get;
      set;
    }
 
    public string Password
    {
      get;
      set;
    }
  }
}

Next, let's create our controller:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using [MyStuff].Common.Utils.Localization;
 
namespace [MyStuff].Website.Login
{
  public partial class Login : System.Web.UI.Page
  {
    private LoginModel loginModel;
 
    public LoginModel PageModel
    {
      get { return loginModel; }
    }   
 
    protected void Page_Load(object senderEventArgs e)
    {
      if(!IsPostBack)
      {
        LocalizableMessageStorage messages = 
          (LocalizableMessageStorage)Application["LocaliazedMessages"];
        Session["LoginModel"= new LoginModel(messages);
      }  
      
      loginModel = (LoginModel)Session["LoginModel"];
 
      Page.DataBind();    
    }
 
    protected void btnLogin_Click(object senderEventArgs e)
    {
      loginModel.UserName = txtUserName.Text;
      loginModel.Password = txtPassword.Text;
 
      //yada yada yada
    }
  }

Then our view. Don't judge my ugly html, it is just a demonstration :=)

<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="Login.aspx.cs" Inherits="[MyStuff].Website.Login.Login" %>
 
<!DOCTYPE html>
 
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
</head>
<body>
    <form id="login" runat="server">
    <div>
      <asp:Label ID="lblTitle" runat="server" Text="<%#PageModel.PageTitle %>"></asp:Label>
      <asp:Label ID="lblUser" runat="server" Text="<%#PageModel.UserLabel %>"></asp:Label>
      <asp:TextBox ID="txtUserName" runat="server"></asp:TextBox>
      <asp:Label ID="lblPassword" runat="server" Text="<%#PageModel.PasswordLabel %>">
      </asp:Label>
      <asp:TextBox ID="txtPassword" runat="server"></asp:TextBox>
      <asp:Button ID="btnLogin" runat="server" OnClick="btnLogin_Click" />
    </div>
    </form>
</body>
</html>

Look familiar? And it is true to the MVC pattern. Now get off my back Microsoft!

Saturday, April 20, 2013

Unlimited Unsigned Integer in C++

For a while now I have been working through Project Euler to keep both my mind sharp and to maintain and improve my C++ skills. C++ is by no means necessary for Project Euler problems, but I have chosen to do all of the problems in C++ when possible for this reason. Anyways, there is a Power Digit Sum question--actually there are several-- and the brute force way of attacking the problem is using an unbounded integer: which the c++ standard library does not implement. So, I set about implementing my own. This is also a very common data structures and algorithms question, so I figured I would go ahead and write my own for the fun of it.  Feel free to reuse this code and improve upon it. If you improve it, however, please submit the patch to me so that I can update my version.

NOTE: It is unethical and against the rules to post solutions to Project Euler problems on any public site. This is not a solution to any Project Euler problems that I know of and is merely a tool that I used in my actual algorithm to solve the problem. 

The design is pretty straight forward. First, I model the data as a simple linked list with the head node as the most significant 32 bits and the tail being the least significant. Then, I implemented all of the bitwise operators. After this, I defined addition using only bit operations, and subtraction using two's complement and addition. Division uses the optimized bit shifts algorithm with subtraction. Multiplication is implemented using peasant multiplication.

Here is the code:

/*
 *Author: Jonathan M. Henson
 *Date: 4/20/2013
 *Description: Linkedlist implementation of an Unlimited Unsigned Integer.
 *
 *This Source Code Form is subject to the terms of the Mozilla Public
 * License, v. 2.0. If a copy of the MPL was not distributed with this
 * file, You can obtain one at http://mozilla.org/MPL/2.0/.
 */
 
#ifndef LIMITLESS_UNSIGNED_H
#define LIMITLESS_UNSIGNED_H
 
#include <cstdlib>
#include <cstdio>
#include <list>
#include <string>
 
class LimitlessUnsigned
{
public:
  LimitlessUnsigned();
  LimitlessUnsigned(const char* strNumber);
  LimitlessUnsigned(unsigned number);
  LimitlessUnsigned(const LimitlessUnsigned&);
  
  LimitlessUnsigned& operator = (const LimitlessUnsigned&);
  LimitlessUnsigned& operator += (const LimitlessUnsigned&);
  LimitlessUnsigned& operator -= (const LimitlessUnsigned&);
  LimitlessUnsigned& operator *= (const LimitlessUnsigned&);
  LimitlessUnsigned& operator /= (const LimitlessUnsigned&);
  
  LimitlessUnsigned operator << (unsigned) const;
  LimitlessUnsigned operator >> (unsigned) const;
  LimitlessUnsigned operator |  (const LimitlessUnsigned&) const;
  LimitlessUnsigned operator & (const LimitlessUnsigned&) const;
  LimitlessUnsigned operator ^ (const LimitlessUnsigned&) const;
  bool operator > (const LimitlessUnsigned&) const;
  bool operator < (const LimitlessUnsigned&) const;
  bool operator == (const LimitlessUnsigned&) const;
  bool operator <= (const LimitlessUnsigned&) const;
  bool operator >= (const LimitlessUnsigned&) const;
  bool operator != (const LimitlessUnsigned&) const;
  LimitlessUnsigned operator + (const LimitlessUnsigned&) const;
  LimitlessUnsigned operator - (const LimitlessUnsigned&) const;
  LimitlessUnsigned operator * (const LimitlessUnsigned&) const;
  LimitlessUnsigned operator / (const LimitlessUnsigned&) const;
  LimitlessUnsigned operator % (const LimitlessUnsigned&) const;
  LimitlessUnsigned& operator ++ ();
  LimitlessUnsigned operator ++ (int);
  LimitlessUnsigned& operator -- ();
  LimitlessUnsigned operator --(int);
  
  std::string ToString(char format);
  
public:
  std::list<unsigned> integerList;
    
private:
  void ShiftLeft(unsigned shifts, bool allowRegisterLoss = false);
  void ShiftRight(unsigned shifts);
  void LogicalOr(const LimitlessUnsigned&);
  void LogicalAnd(const LimitlessUnsigned&);
  void XOR(const LimitlessUnsigned&);
  void Not();
  void TwosComp();
  bool IsLessThan(const LimitlessUnsigned&) const;
  bool IsGreaterThan(const LimitlessUnsigned&) const;
  bool Equals(const LimitlessUnsigned&) const;
  void Add(const LimitlessUnsigned&, bool allowRegisterLoss = false);
  void Sub(const LimitlessUnsigned&);
  void Mul(const LimitlessUnsigned&);
  void Div(const LimitlessUnsigned&);
  
  static void SetMatchingWidth(std::list<unsigned>&, std::list<unsigned>&);
  static void TrimFront(std::list<unsigned>&);
  
  static LimitlessUnsigned Divide(const LimitlessUnsigned& dividend, const LimitlessUnsigned& divisor);
  static LimitlessUnsigned EfficientBigMod(const LimitlessUnsigned& dividend, const LimitlessUnsigned& divisor);
  
};
 
#endif
 
/*
 *Author: Jonathan M. Henson
 *Date: 4/20/2013
 *Description: Linkedlist implementation of an Unlimited Unsigned Integer.
 *
 *This Source Code Form is subject to the terms of the Mozilla Public
 * License, v. 2.0. If a copy of the MPL was not distributed with this
 * file, You can obtain one at http://mozilla.org/MPL/2.0/. 
 */
 
#include "LimitlessUnsigned.h"
#include <sstream>
#include <iomanip>
 
unsigned CARRY_INT = 0x00000001;
unsigned CARRY_INT_HIGH = 0x80000000;
#define ZERO 0
 
LimitlessUnsigned::LimitlessUnsigned()
{
  unsigned first32 = 0;
  integerList.push_back(first32);
}
 
LimitlessUnsigned::LimitlessUnsigned(const char* strNumber)
{  
  LimitlessUnsigned temp;
  integerList = temp.integerList;
  
  if(!strNumber)
    return;
     
  while(char val = *strNumber++)
  {
    if(val == '-' || val == '+')
      continue;
      
    temp = temp * 10u + ((*strNumber) - '0');
  }
  
  integerList = temp.integerList;
}
 
LimitlessUnsigned::LimitlessUnsigned(unsigned number)
{
  unsigned first32 = number;
  integerList.push_back(first32);
}
 
LimitlessUnsigned::LimitlessUnsigned(const LimitlessUnsigned& other)
{
  integerList = other.integerList;
}
 
LimitlessUnsigned& LimitlessUnsigned::operator = (const LimitlessUnsigned& other)
{
  integerList = other.integerList;
  return *this;
}
 
LimitlessUnsigned& LimitlessUnsigned::operator += (const LimitlessUnsigned& other)
{
  LimitlessUnsigned tmpOther = other;
  SetMatchingWidth(integerList, tmpOther.integerList);
  Add(tmpOther);
  TrimFront(integerList);
  return *this;
}
 
LimitlessUnsigned& LimitlessUnsigned::operator -= (const LimitlessUnsigned& other)
{
  LimitlessUnsigned tmpOther = other;
  SetMatchingWidth(integerList, tmpOther.integerList);
  Sub(tmpOther);
  TrimFront(integerList);
  return *this;
}
  
LimitlessUnsigned& LimitlessUnsigned::operator *= (const LimitlessUnsigned& other)
{
  LimitlessUnsigned tmpOther = other;
  SetMatchingWidth(integerList, tmpOther.integerList);
  Mul(other);
  TrimFront(integerList);
  return *this;
}
  
LimitlessUnsigned& LimitlessUnsigned::operator /= (const LimitlessUnsigned& other)
{
  LimitlessUnsigned tmpOther = other;
  SetMatchingWidth(integerList, tmpOther.integerList);
  Div(other);
  TrimFront(integerList);
  return *this;
}
 
LimitlessUnsigned LimitlessUnsigned::operator << (unsigned shifts) const
{
  LimitlessUnsigned temp = *this;
  temp.ShiftLeft(shifts, false);
  TrimFront(temp.integerList);
  return temp;
}
 
LimitlessUnsigned LimitlessUnsigned::operator >> (unsigned shifts) const
{
  LimitlessUnsigned temp = *this;
  temp.ShiftRight(shifts);
  TrimFront(temp.integerList);
  return temp;
}
 
LimitlessUnsigned LimitlessUnsigned::operator |  (const LimitlessUnsigned& other) const
{
  LimitlessUnsigned temp = *this;
  LimitlessUnsigned tmpOther = other;
  SetMatchingWidth(temp.integerList, tmpOther.integerList);
  temp.LogicalOr(tmpOther);
  TrimFront(temp.integerList);
  return temp;
}
 
LimitlessUnsigned LimitlessUnsigned::operator & (const LimitlessUnsigned& other) const
{  
  LimitlessUnsigned temp = *this;
  LimitlessUnsigned tmpOther = other;
  SetMatchingWidth(temp.integerList, tmpOther.integerList);
  temp.LogicalAnd(tmpOther);
  TrimFront(temp.integerList);
  
  return temp;
}
 
LimitlessUnsigned LimitlessUnsigned::operator ^ (const LimitlessUnsigned& other) const
{
  LimitlessUnsigned temp = *this;
  LimitlessUnsigned tmpOther = other;
  SetMatchingWidth(temp.integerList, tmpOther.integerList);
  temp.XOR(other);
  TrimFront(temp.integerList);
  
  return temp;
}
 
bool LimitlessUnsigned::operator > (const LimitlessUnsigned& other) const
{
  LimitlessUnsigned temp = *this;
  LimitlessUnsigned tmpOther = other;
  SetMatchingWidth(temp.integerList, tmpOther.integerList);
  return temp.IsGreaterThan(tmpOther);
}
 
bool LimitlessUnsigned::operator < (const LimitlessUnsigned& other) const
{
  LimitlessUnsigned temp = *this;
  LimitlessUnsigned tmpOther = other;
  SetMatchingWidth(temp.integerList, tmpOther.integerList);
  return temp.IsLessThan(tmpOther);
}
 
bool LimitlessUnsigned::operator == (const LimitlessUnsigned& other) const
{
  LimitlessUnsigned temp = *this;
  LimitlessUnsigned tmpOther = other;
  SetMatchingWidth(temp.integerList, tmpOther.integerList);
  return temp.Equals(tmpOther);
}
 
bool LimitlessUnsigned::operator <= (const LimitlessUnsigned& other) const
{
  return *this < other || *this == other;
}
 
bool LimitlessUnsigned::operator >= (const LimitlessUnsigned& other) const
{
  return *this > other || *this == other;  
}
  
bool LimitlessUnsigned::operator != (const LimitlessUnsigned& other) const
{
  return !(*this == other);
}
 
LimitlessUnsigned LimitlessUnsigned::operator + ( const LimitlessUnsigned& other) const
{
  LimitlessUnsigned temp = *this;
  LimitlessUnsigned tmpOther = other;
  SetMatchingWidth(temp.integerList, tmpOther.integerList);
  temp.Add(tmpOther, false);
  TrimFront(temp.integerList);
  return temp;
}
 
LimitlessUnsigned LimitlessUnsigned::operator * (const LimitlessUnsigned& other) const
{
  LimitlessUnsigned temp = *this;
  LimitlessUnsigned tmpOther = other;
  SetMatchingWidth(temp.integerList, tmpOther.integerList);
  temp.Mul(tmpOther);
  TrimFront(temp.integerList);
  return temp;
}
 
LimitlessUnsigned LimitlessUnsigned::operator / ( const LimitlessUnsigned& other) const
{
  LimitlessUnsigned temp = *this;
  LimitlessUnsigned tmpOther = other;
  SetMatchingWidth(temp.integerList, tmpOther.integerList);  
  temp.Div(tmpOther);
  TrimFront(temp.integerList);
  return temp;
}
 
LimitlessUnsigned LimitlessUnsigned::operator % (const LimitlessUnsigned& other) const
{
  LimitlessUnsigned temp = *this;
  LimitlessUnsigned tmpOther = other;
  SetMatchingWidth(temp.integerList, tmpOther.integerList);
  LimitlessUnsigned retVal = EfficientBigMod(temp, tmpOther);
  TrimFront(temp.integerList);
  
  return retVal;
}
 
LimitlessUnsigned LimitlessUnsigned::operator - (const LimitlessUnsigned& other) const
{
  LimitlessUnsigned temp = *this;
  LimitlessUnsigned tmpOther = other;
  SetMatchingWidth(temp.integerList, tmpOther.integerList);
  temp.Sub(tmpOther);
  TrimFront(temp.integerList);
  
  return temp;
}
 
LimitlessUnsigned& LimitlessUnsigned::operator ++ ()
{  
  *this = *this + 1u;
  return *this;
}
 
LimitlessUnsigned LimitlessUnsigned::operator ++ (int)
{
  LimitlessUnsigned temp = *this;
  *this = *this + 1u;
  
  return temp;
}
 
LimitlessUnsigned& LimitlessUnsigned::operator -- ()
{
  *this = *this - 1u;
  return *this; 
}
  
LimitlessUnsigned LimitlessUnsigned::operator --(int)
{
  LimitlessUnsigned temp = *this;
  *this = *this - 1u;
  
  return temp;
}
 
std::string LimitlessUnsigned::ToString(char format)
{
  std::stringstream ss;    
  
  if(format == 'x' || format == 'h')
  {
      for(std::list<unsigned>::iterator iter = integerList.begin(); iter != integerList.end(); iter++)
      {
        if(format == 'x' || format == 'h')
        {
          ss << std::hex << std::setfill('0') << std::setw (8);
          ss << *iter;
        }
      }
  
    ss.str("0x" + ss.str());
    return ss.str();
  }  
  
  LimitlessUnsigned temp = *this;
  std::string buffer;
  while(temp > 0u)
  {
    buffer = (char)((temp % 10u).integerList.back() + '0') + buffer;
    temp = temp / 10u;  
  }
  
  
  return buffer;
}
 
void LimitlessUnsigned::ShiftLeft(unsigned shifts, bool allowRegisterLoss)
{
  unsigned carry = 0u;
  bool front_carry = false;
  
  while(shifts > 0u)
  {    
    if((integerList.front() & CARRY_INT_HIGH) == CARRY_INT_HIGH)
      front_carry = true;     
        
    for(std::list<unsigned>::reverse_iterator iter = integerList.rbegin(); iter != integerList.rend(); ++iter)
    {      
      unsigned temp = *iter;
      
      *iter = *iter << 1;
      *iter = *iter | carry;       
      
      if((temp & CARRY_INT_HIGH) == CARRY_INT_HIGH)
        carry = CARRY_INT;
      else
        carry = 0u;
    }
    
    carry = 0u;
    
    if(front_carry && !allowRegisterLoss)
    {
      front_carry = false;
      integerList.push_front(1u);
    }
    
    --shifts;    
  }
}
 
void LimitlessUnsigned::ShiftRight(unsigned shifts)
{
  int carry = 0u;
  
  while(shifts > 0u)
  {
    for(std::list<unsigned>::iterator iter = integerList.begin(); iter != integerList.end(); ++iter)
    {  
      unsigned temp = *iter;
      *iter = *iter >> 1;
      *iter = *iter | carry;
          
      if((temp & CARRY_INT) == CARRY_INT)
        carry = CARRY_INT_HIGH;
      else
        carry = 0u;
    }
    
    --shifts;  
  }
}
 
void LimitlessUnsigned::LogicalOr(const LimitlessUnsigned& other)
{
   std::list<unsigned> cpyOtherList = other.integerList;
    
   std::list<unsigned>::reverse_iterator thisIter = integerList.rbegin();
   
   for(std::list<unsigned>::reverse_iterator iter = cpyOtherList.rbegin(); iter != cpyOtherList.rend(); ++iter)
   {
      *thisIter = *thisIter | *iter;
      ++thisIter;
   }
}
 
void LimitlessUnsigned::LogicalAnd(const LimitlessUnsigned& other)
{
  std::list<unsigned> cpyOtherList = other.integerList;
  
  std::list<unsigned>::reverse_iterator thisIter = integerList.rbegin();
   
   for(std::list<unsigned>::reverse_iterator iter = cpyOtherList.rbegin(); iter != cpyOtherList.rend(); ++iter)
   {
      *thisIter = *thisIter & *iter;
      ++thisIter;
   }  
    
}
 
void LimitlessUnsigned::XOR(const LimitlessUnsigned& other)
{
  std::list<unsigned> cpyOtherList = other.integerList;
  
  std::list<unsigned>::reverse_iterator thisIter = integerList.rbegin();
  
   for(std::list<unsigned>::reverse_iterator iter = cpyOtherList.rbegin(); iter != cpyOtherList.rend(); ++iter)
   {
      *thisIter = *thisIter ^ *iter;
      ++thisIter;
   }
}
 
void LimitlessUnsigned::Not()
{  
  for(std::list<unsigned>::iterator iter = integerList.begin(); iter != integerList.end(); ++iter)
   {      
      *iter = ~*iter;      
   }   
}
 
void LimitlessUnsigned::TwosComp()
{
  LimitlessUnsigned temp = 1u;
  
  SetMatchingWidth(temp.integerList, integerList);
  
  Not();
  Add(temp, true);
}
 
bool LimitlessUnsigned::IsGreaterThan(const LimitlessUnsigned& other) const
{
  if(Equals(other))
    return false;
  
  std::list<unsigned> listCpy = integerList;
  std::list<unsigned>::iterator thisIter = listCpy.begin();
  
  for(std::list<unsigned>::const_iterator iter = other.integerList.begin(); iter != other.integerList.end(); ++iter)
  {
    if(*thisIter > *iter)
      return true;
  
    if(*thisIter < *iter)
      return false;
    
    ++thisIter;
  }
  
  return false;
}
 
bool LimitlessUnsigned::IsLessThan(const LimitlessUnsigned& other) const
{
  if(Equals(other))
    return false;
  
  std::list<unsigned> listCpy = integerList;
  std::list<unsigned>::iterator thisIter = listCpy.begin();  
  
  for(std::list<unsigned>::const_iterator iter = other.integerList.begin(); iter != other.integerList.end(); ++iter)
  {
    if(*thisIter < *iter)
      return true;
    
    if(*thisIter > *iter)
      return false;
    
    ++thisIter;
  }
  
  return false;
}
 
bool LimitlessUnsigned::Equals(const LimitlessUnsigned& other) const
{
  
  std::list<unsigned> listCpy = integerList;
  std::list<unsigned>::iterator thisIter = listCpy.begin();
  
  for(std::list<unsigned>::const_iterator iter = other.integerList.begin(); iter != other.integerList.end(); ++iter)
  {
    if(*thisIter != *iter)
      return false;
    
    ++thisIter;
  }
  
  return true;
}
 
void LimitlessUnsigned::Add(const LimitlessUnsigned& other, bool allowRegisterLoss)
{
  LimitlessUnsigned carry = *this;
  carry = carry & other;
  LimitlessUnsigned result = *this ^ other;
  
  SetMatchingWidth(carry.integerList, result.integerList);
  while(carry != 0u)
  {
    carry.ShiftLeft(1, allowRegisterLoss);
    LimitlessUnsigned shiftedcarry = carry;
    carry = result & shiftedcarry;    
    result = result ^ shiftedcarry;
    SetMatchingWidth(carry.integerList, result.integerList);
  }
  
 *this = result;
}
 
void LimitlessUnsigned::Sub(const LimitlessUnsigned& other)
{
  if(*this <= other)
  {
    *this = 0u;
    return;
  }
  
  LimitlessUnsigned temp = other;  
  
  temp.TwosComp();
  Add(temp, true);
}
  
void LimitlessUnsigned::Mul(const LimitlessUnsigned& other)
{
  LimitlessUnsigned tempOther = other;  
  LimitlessUnsigned prod = 0u;
  
  while(*this > 0u)
  {
    if((*this & 1u) == 1u)
      prod += tempOther;
    
    *this = *this >> 1;
    tempOther = tempOther << 1;
  }
  
  *this = prod;
}
   void LimitlessUnsigned::Div(const LimitlessUnsigned& other) {   *this = Divide(*this, other); } void LimitlessUnsigned::SetMatchingWidth(std::list<unsigned>& listA, std::list<unsigned>& listB) {   while(listA.size() > listB.size())   {     listB.push_front(0);   }      while(listB.size() > listA.size())   {     listA.push_front(0);   } } void LimitlessUnsigned::TrimFront(std::list<unsigned>& list) {   while(list.size() > 1 && list.front() == 0)   {     list.pop_front();   } } LimitlessUnsigned LimitlessUnsigned::Divide(const LimitlessUnsigned& dividend, const LimitlessUnsigned& divisor) {   LimitlessUnsigned quotient = 1u;   LimitlessUnsigned temp = divisor;      if(dividend < temp)   {     return 0u;   }   else if(temp == dividend)   {     return 1u;   }      while(temp <= dividend)   {     temp = temp << 1;     quotient = quotient << 1;   }      temp = temp >> 1;   quotient = quotient >> 1;         return quotient += Divide(dividend - temp, divisor); } LimitlessUnsigned LimitlessUnsigned::EfficientBigMod(const LimitlessUnsigned& dividend, const LimitlessUnsigned& divisor) {   LimitlessUnsigned quotient = 1u;   LimitlessUnsigned temp = divisor;      if(dividend < temp)     return dividend;   else if(temp == dividend)     return 0u;      while(temp <= dividend)   {     temp = temp << 1;   }      temp = temp >> 1;      return EfficientBigMod(dividend - temp, divisor); }