using System; class BWC { static void Main() { int[] values = { 0, 0x111, 0xfffff, 0x8888, 0x22000022 }; foreach (int v in values) { Console.WriteLine("~0x{0:x8} = 0x{1:x8}", v, ~v); } } }
Author: coder
Get Ordinal
#region License
// Copyright (c) 2007 James Newton-King
//
// Permission is hereby granted, free of charge, to any person
// obtaining a copy of this software and associated documentation
// files (the “Software”), to deal in the Software without
// restriction, including without limitation the rights to use,
// copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the
// Software is furnished to do so, subject to the following
// conditions:
//
// The above copyright notice and this permission notice shall be
// included in all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED “AS IS”, WITHOUT WARRANTY OF ANY KIND,
// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
// OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
// NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
// HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
// WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
// OTHER DEALINGS IN THE SOFTWARE.
#endregion
using System;
using System.Globalization;
namespace Newtonsoft.Utilities.Text
{
public class FormatUtils
{
public static string GetOrdinal(int value)
{
string[] _suffixes = new string[] { “th”, “st”, “nd”, “rd” };
int tenth = value%10;
if (tenth >= _suffixes.Length)
{
return _suffixes[0];
}
else
{
// special case for 11, 12, 13
int hundredth = value % 100;
if (hundredth >= 11 && hundredth <= 13)
return _suffixes[0];
return _suffixes[tenth];
}
}
}
}
[/csharp]
Converts an integer into a roman numeral.
//http://advancementvoyage.codeplex.com/ //Microsoft Public License (Ms-PL) using System; using System.Collections.Generic; using System.Linq; using System.Text; namespace AdvancementVoyage.Magic.Utility { /// <summary> /// Extension methods for the int type. /// </summary> public static class IntExtensions { /// <summary> /// Converts an integer into a roman numeral. /// </summary> /// <param name="number"> /// The number being transformed. /// </param> /// <returns> /// A string representation of the number's corresponding roman numeral. /// </returns> public static string ToRomanNumeral(this int number) { var retVal = new StringBuilder(5); var valueMap = new SortedDictionary<int, string> { { 1, "I" }, { 4, "IV" }, { 5, "V" }, { 9, "IX" }, { 10, "X" }, { 40, "XL" }, { 50, "L" }, { 90, "XC" }, { 100, "C" }, { 400, "CD" }, { 500, "D" }, { 900, "CM" }, { 1000, "M" }, }; foreach (var kvp in valueMap.Reverse()) { while (number >= kvp.Key) { number -= kvp.Key; retVal.Append(kvp.Value); } } return retVal.ToString(); } } }
Int extension Convert integer to boolean, is it even, is it positive
//http://advancementvoyage.codeplex.com/ //Microsoft Public License (Ms-PL) using System; using System.Collections.Generic; using System.Linq; using System.Text; namespace AroLibraries.ExtensionMethods { public static class IntExt { public static IEnumerable<int> Ext_IterateTo(this int start, int end) { var diff = end - start > 0 ? 1 : -1; for (var current = start; current != end; current += diff) { yield return current; } } public static bool Ext_ToBool(this int i) { if (i > 0) return true; return false; } public static bool Ext_IsEven(this int iInt) { if ((iInt % 2) == 0) { return true; } return false; } public static bool Ext_IsPositive(this int iInt) { if (iInt > 0) return true; return false; } } }
Calculate average for a list of integer values with params int[] values
class TestParams
{
private static void Average(string title, params int[] values)
{
int sum = 0;
for (int i = 0; i < values.Length; i++)
{
sum += values[i];
System.Console.Write(values[i] + ", ");
}
System.Console.WriteLine((float)sum/values.Length);
}
static void Main()
{
Average ("List One", 5, 10, 15);
Average ("List Two", 5, 10, 15, 20, 25, 30);
}
}
[/csharp]
Write Synch safe Int32
//http://walkmen.codeplex.com/license // License: GNU General Public License version 2 (GPLv2) using System; using System.Collections.Generic; using System.Text; namespace Walkmen.Util { public sealed class NumericUtils { private NumericUtils() { } public static byte[] WriteSynchsafeInt32(int value) { byte[] result = new byte[4]; result[0] = (byte)((value & 0xFE00000) >> 21); result[1] = (byte)((value & 0x01FC000) >> 14); result[2] = (byte)((value & 0x0003F80) >> 7); result[3] = (byte)((value & 0x000007F)); return result; } } }
Read sync safe int 32
//http://walkmen.codeplex.com/license
// License: GNU General Public License version 2 (GPLv2)
using System;
using System.Collections.Generic;
using System.Text;
namespace Walkmen.Util
{
public sealed class NumericUtils
{
private NumericUtils()
{
}
public static int ReadSynchsafeInt32(byte[] buffer, int offset)
{
return ((buffer[offset + 0] & 0x7F) << 21) | ((buffer[offset + 1] & 0x7F) << 14) | ((buffer[offset + 2] & 0x7F) << 7) | (buffer[offset + 3] & 0x7f);
}
}
}
[/csharp]