Post: [C#] BitwiseOperation class
06-03-2011, 04:44 AM #1
kiwimoosical
Bounty hunter
(adsbygoogle = window.adsbygoogle || []).push({}); Meh, this is a class I've had for a while now, added like 2 things today but I just thought it would be useful to some people. Btw, the 'Quick' functions are all much much faster than the corresponding Math.Whatever function. Just saying.

    using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Bitwise
{
public static class BitwiseStuff
{
public static void TurnBitOn(int position, ref int data)
{
if (position > Cool Man (aka Tustin)
throw new Exception("Position cannot be greater than 8");

data |= (1 << position);
}

public static void TurnBitOff(int position, ref int data)
{
if (position > Cool Man (aka Tustin)
throw new Exception("Position cannot be greater than 8");

data &= ~(1 << position);
}

public static void SwapInts(ref int a, ref int b) //no temp BS
{
a ^= b;
b ^= a;
a ^= b;
}

public static void QuickSwapSign(ref int a)
{
a = ~a + 1;
}

public static bool QuickIsEven(int a)
{
return (a & 1) == 0;
}

public static bool QuickIsOdd(int a)
{
return !QuickIsEven(a);
}

public static void QuickModuloDivide(ref int a, int mod)
{
int temp = (mod - 1);
a &= temp;
}

public static int QuickAbsoluteValue(int a)
{
return (a < 0) ? -a : a;
}

public static int QuickFloor(double x)
{
return (int)x | 0;
}

public static int QuickFloor(float x)
{
return (int)x | 0;
}

public static int QuickCeiling(float x)
{
return QuickFloor(x + 1);
}

public static int QuickCeiling(double x)
{
return QuickFloor(x + 1);
}

public static double QuickRound(float d)
{
return (d - (int)d) < .5 ?
(int)d | 0 :
(int)(d + 1) | 0 ;
}

public static double QuickRound(double d)
{
return (d - (int)d) < .5 ?
(int)d | 0 :
(int)(d + 1) | 0 ;
}
}
}
(adsbygoogle = window.adsbygoogle || []).push({});
06-03-2011, 12:25 PM #2
kiwimoosical
Bounty hunter
Fixed round function.

Copyright © 2026, NextGenUpdate.
All Rights Reserved.

Gray NextGenUpdate Logo