Is an int in C# 32 bits? [closed] - c#

It's difficult to tell what is being asked here. This question is ambiguous, vague, incomplete, overly broad, or rhetorical and cannot be reasonably answered in its current form. For help clarifying this question so that it can be reopened, visit the help center.
Closed 10 years ago.
I have just heard that the size of an int is machine dependant. I have always thought that the size of an int is 32 bits. Can someone explain this conundrum?

In C#, the int type is always an Int32, and is always 32 bits.
Some other languages have different rules, and int can be machine dependent. In certain languages, int is defined as having a minimum size, but a machine/implementation specific size at least that large. For example, in C++, the int datatype is not necessarily 32 bits. From fundimental data types:
the general specification is that int has the natural size suggested by the system architecture (one "word") and the four integer types char, short, int and long must each one be at least as large as the one preceding it, with char being always one byte in size.
However, .NET standardized this, so the types are actually specified as Int32, Int64, etc. In C#, int is an alias for System.Int32, and will always be 32 bits. This is guaranteed by section 4.1.5 Integral Types within the C# Language Specification, which states:
The int type represents signed 32-bit integers with values between –2147483648 and 2147483647.

MSDN states that int is 32 bits. Was the person talking about C++ perhaps?
In C++ size of integral types in bytes is unspecified by the standard

From the C# Language Specifications:
The int type represents signed 32-bit integers with values between
–2147483648 and 2147483647.

int is an alias of System.Int32. You also can use System.Int16, System.Int64 and BigInteger.

Related

Multiplication of two 64 digit nos [closed]

It's difficult to tell what is being asked here. This question is ambiguous, vague, incomplete, overly broad, or rhetorical and cannot be reasonably answered in its current form. For help clarifying this question so that it can be reopened, visit the help center.
Closed 10 years ago.
How can I multiply two 64 digit decimal numbers together in C# without overflowing?
If you need 64 decimal digit numbers, and if they are integers, you can use System.Numerics.BigInteger struct (needs a reference to System.Numerics.dll assembly).
BigInteger firstNumber = BigInteger.Parse("63518439492097324687235465876298368764576527346564625480");
BigInteger secondNumber = BigInteger.Parse("84890247648975285765484902890273086475254764765147643611");
BigInteger product = firstNumber * secondNumber;
You could always use decimal to store the result of multiplication of two long numbers.
long bigNumber = 12345678L;
long anotherBigNumber = 23456789L;
decimal result = bigNumber * anotherBigNumber;
I think you can do the calculation of any number (as long as you want to) IF first that number you convert into string (so two separate string) than get the last number (character) and multiply it with every number from the other character (just like you learned in school) and so on and you add that to you final score number (also as a string). I know this can be done and it would be king difficult logic at the beginning but once you create this function you can multiply any number you want :)
If they are integers, use the BigInteger class in System.Numerics (Reference System.Numerics.dll!)
If not, there is a BigRational class in Codeplex. It was meant to be in System.Numerics but it got kicked. Link:
http://bcl.codeplex.com/releases/view/42782

Why should I use Integers when I could just use floats or doubles in C#? [closed]

It's difficult to tell what is being asked here. This question is ambiguous, vague, incomplete, overly broad, or rhetorical and cannot be reasonably answered in its current form. For help clarifying this question so that it can be reopened, visit the help center.
Closed 10 years ago.
Just learning C# at the moment.
I don't get why anyone would use Integers when they could use floats or doubles..
Floats will add/subtract whole numbers AND decimal numbers so why would anyone ever bother using a plain old integer?
Seems like floats or double will take care of anything that an Integer can do with the bonus of being able to handle . numbers too..
Thanks!
The main reason is the same reason we often prefer to use integer fractions instead of fixed-precision decimals. With rational fractions, (1/3) times 3 is always 1. (1/3) plus (2/3) is always 1. (1/3) times 2 is (2/3).
Why? Because integer fractions are exact, just like integers are exact.
But with fixed-precision real numbers -- it's not so pretty. If (1/3) is .33333, then 3 times (1/3) will not be 1. And if (2/3) is .66666, then (1/3)+(2/3) will not be one. But if (2/3) is .66667, then (1/3) times 2 will not be (2/3) and 1 minus (1/3) will not be (2/3).
And, of course, you can't fix this by using more places. No number of decimal digits will allow you to represent (1/3) exactly.
Floating point is a fixed-precision real format, much like my fixed-precision decimals above. It doesn't always follow the naive rules you might expect. See the classic paper What Every Computer Scientist Should Know About Floating-Point Arithmetic.
To answer your question, to a first approximation, you should use integers whenever you possibly can and use floating point numbers only when you have to. And you should always remember that floating point numbers have limited precision and comparing two floating point numbers to see if they are equal can give results you might not expect.
As you can see in here the different types each have their own size.
For example, when working with big databases, an int or float may double up the required size.
You have different datatypes because they use a different amount of bits to store data.
Typically an integer will use less memory than a double, that is why one doesn't just use the largest possible datatype.
http://en.wikipedia.org/wiki/Data_type
Most of the answer given here deal directly with math concepts. There are purely computational reasons for using integers. several jump to mind:
loop counters, yes I know you can a decimal/double in the for loop but really do you need that degree of complexity?
internal enumeration values
array indices
There are several reasons. Performance, memory, even the desire to not see decimals (without having to play with format strings).
Floating point:
In computing, floating point describes a method of representing an approximation to real numbers in a way that can support a wide range of values. Numbers are, in general, represented approximately to a fixed number of significant digits and scaled using an exponent.
Integer:
In computer science, an integer is a datum of integral data type, a data type which represents some finite subset of the mathematical integers.
So, precision is one argument.
There are several reasons. First off, like people have already said, double stores 64-bit numeric values, while int only requires 32-bit.
Float is a different case. Both int and float store 32-bit numbers, but float is less precise. A float value is precise up to 7 digits, but beyond that it is just an approximation. If you have larger numbers, or if there is some case where you purposefully want to force only integer values with no fractional numbers, int is the way to go. If you don't care about loss of precision and want to allow a wider range of values, you can use float instead.
The primary reason for using integers is memory consumption and performance.
doubles are in most cases stored in a 64b memory blocks (compared to 32b for int)and use a somewhat complicated standard for representation (in some cases approximation of the real values)
complicated representation that requires calculation of mantis and exponent.
in most cases it requires use of dedicated coprocessor for floating point arithmetic
for integers complements and shifting can be used in order to speed up the arithmetic operations.
a number of use cases where it is more appropriate (natural) to use integers like for indexing arrays, loops, for getting reminder of the division, counting etc.
Also if you would need to do all of this using types for representing real numbers your code would be more more error prone.

Creating long randoms [closed]

It's difficult to tell what is being asked here. This question is ambiguous, vague, incomplete, overly broad, or rhetorical and cannot be reasonably answered in its current form. For help clarifying this question so that it can be reopened, visit the help center.
Closed 10 years ago.
So I have this
Random random1 = new Random();
int intrandom1 = random1.Next();
I want to put a long after the .Next. How do I do that? It only accepts ints.
First idea: A long as 64 bit integer, is the combination of 2 32 bit integers, so you can use:
((long)random1.Next() << 32) | random1.Next()
Or perhaps
((long)random1.Next() <<< 32) | random1.Next()
if you use java (?) and need a unsigned shift
edit: does not look like Java. Java has random1.nextLong() for that. Perhaps C#? I do not know that
It can be generated arbitrary long number by using a simple linked list. Just imagine that every node from list can store a number random generated, and a function can be read that list like an unitary number. Using an algorithm like that you can obtain an arbitrary long random number.

What is the lightest collection for store these values? [closed]

It's difficult to tell what is being asked here. This question is ambiguous, vague, incomplete, overly broad, or rhetorical and cannot be reasonably answered in its current form. For help clarifying this question so that it can be reopened, visit the help center.
Closed 10 years ago.
I have many objects (such as 10.000, more or less). Every object has3 values :
Index (decimal, such as 0,0 <= X <= 100.000,9);
A Latitude value;
A Longitude value;
and I need to perform some search due to the Index value. Which will be the light approch to this? List<MyObject>? I know there are hashtable, but only for 2 values...
I read these values from a .csv file and I'll store it on application. WebForm, .NET 4.5.
The very lightest approach in terms of memory use is to put these into a struct, and hold them in an array of such structs. From what you say, you can't really pack data any tighter than that: two doubles and a decimal will occupy 32 bytes per entry, and the array of structs does not add any per-item overhead on top of this.
Having said that, this will slow down your coding and might save too little to matter in practice.
Why don't you use a Dictionary like this:
public class Position
{
public Latitude Latitude { get ; set ; }
public Longitude Longitude { get ; set ; }
}
public Dictionary<decimal,Position> Positions ;
Or use a Tuple in the dictionary:
public Dictionary<decimal,Tuple<Latitude,Longitude>> Positions ;
I believe the absolutely lightest approach would be bitmasking your values into an unsigned long, though it is slightly cumbersome.
To really get the grasp on which is the most efficient approach, i recommend trying them all with test values and looking at the output of sizeof() on them. that way you'd be really sure what's their runtime memory size.
I'd suggest a custom struct to hold your values, a tuple could work as well.

Array containing only 0 and 1 values [closed]

It's difficult to tell what is being asked here. This question is ambiguous, vague, incomplete, overly broad, or rhetorical and cannot be reasonably answered in its current form. For help clarifying this question so that it can be reopened, visit the help center.
Closed 10 years ago.
I need an array which will only contain the values 0 and 1. Will bool[] be good enough for me? Or is there something lighter weight?
EDIT:
I dont have memory constraints but that array is made and passes online All The Time with big files passes concurrently with that array. I want the maximun optimization so the big files wont be delayed
A bool is probably not the best way to do it. Depends how many numbers you have got.
It is important to realise that even though a bool is a single bit, it requires a full byte in memory.
A BitArray on the other hand takes care of this for you and is more space efficient, although ever so slightly less time efficient.
http://msdn.microsoft.com/en-us/library/system.collections.bitarray.aspx
Depends on your constraints, if it is not for a constrained environment a bool array will work just fine.
Bool Array is good enough. You can consider "false" as 0 and "true" as 1.
May be you need BitArray, the sequence of 1 and 0.
bool[] would do the trick... If your 0 & 1 numbers are in fact just "Flags" and not real numbers.
Using an Enum with the Flags attribute is another option. This would allow you to have an Intention Revealing Name for both boolean values.
[Flags()]
public enum TheFlags
{
NoneSet = 0,
FirstSet = 1,
SecondSet = 2
}
Then you can check if "First" is set like so:
TheFlags flags = TheFlags.FirstSet;
if (flags.HasFlag(TheFlags.FirstSet))
Console.WriteLine("First flag is set!");

Categories