4

In this code, I have a requirement like below:

decimal col;

if (condition)
{
    (decimal)col = (decimal)col | ((decimal)System.Math.Pow(2, 0)); 
}

When this code is compiled, I get an error saying

Operator | cannot be applied to operands of type 'decimal' and 'decimal'

Can anyone explain, please? Thanks

Ondrej Janacek
  • 12,486
  • 14
  • 59
  • 93
sanar
  • 437
  • 9
  • 22

2 Answers2

8

Big numbers

As the compile-time-error says, you cannot use | on decimal. The decimal type is for numbers like 12.34. If you're only using numbers like 12 or 34 then you may be better off using int (between ±2 billion) or long (between ±9 quintillion) depending how big your integer is.

Any number raised to the power zero, n0 equals 1 so your Math.Pow(2, 0) can be replaced by 1.

Your code could then look like this, which will compile.

int col;

and

if (condition)
    col = col | 1;

Finally, you may prefer to use the ternary operator ?: to in-line your if statement, do whichever is more readable in your situation:

col = condition ? (col | 1) : col;

Really big numbers

Using .Net 4.0 or above

If you need to use REALLY BIG integers, there is a class for that! System.Numerics.BigInteger (in System.Numerics.dll).

The BigInteger type is an immutable type that represents an arbitrarily large integer whose value in theory has no upper or lower bounds.

Your declaration would then become:

BigInteger col;

And your bitwise operations as normal.

Using .Net 2.0 or above

Check out the project IntX

IntX is an arbitrary precision integers library written in pure C# 2.0 with fast - about O(N * log N) - multiplication/division algorithms implementation. It provides all the basic arithmetic operations on integers, comparing, bitwise shifting etc.

Reference: Big integers in C#

Community
  • 1
  • 1
dav_i
  • 27,509
  • 17
  • 104
  • 136
  • Forgot to mention that this code runs in a loop; so 0 in Math.Pow(2, 0) will become 1 in next iteration and so on... Also, for my requirement, I need to handle very big numbers, and nothing less than decimal will do... – sanar Dec 02 '13 at 12:25
  • 1
    If you're going to be using huge numbers, decimal is actually a poor choice. It's got a smaller range and higher precision than double. If you gave some more information on actual values we could be more helpful. Are you even going to be using decimal values, or are these just very large integers? – steveg89 Dec 02 '13 at 12:36
  • I (have to) use .NET 2.0 :(.. Also, this code is something which will run inside an SSIS package (SQL Server 2008)... so not sure if I can use fancy, latest namespaces / types – sanar Dec 02 '13 at 13:00
  • @NetTechie Edited with a .Net 2.0 option. Are you sure your number are going to be larger than `9,223,372,036,854,775,807` (`long.MaxValue`) though? – dav_i Dec 02 '13 at 13:15
-1

Cast it to Int64 then apply | operator.

decimal d1 = ..;
decimal d2 = ..;

var res = (long)d1 | (long)d2;
Sriram Sakthivel
  • 72,067
  • 7
  • 111
  • 189