integer

C# Convert Integers into Written Numbers

Is there an efficient method of converting an integer into the written numbers for example: String Written = IntegerToWritten(21); would return "Twenty One" Is there any way of doing this that doesn't involve a massive lookup table? ...

Java: Best way of converting List<Integer> to List<String>

I have a Java list of integers, List<Integer> and I'd like to convert all the integer objects into strings, thus finishing up with a new List<String>. Naturally, I could create a new List and loop through and String.valueOf() all the integers, but I was wondering if there was a better (read: more automatic) way of doing it? [Minor edit...

How to keep a "things done" count in a recursive algorithm in Java?

I have a recursive algorithm which steps through a string, character by character, and parses it to create a tree-like structure. I want to be able to keep track of the character index the parser is currently at (for error messages as much as anything else) but am not keen on implementing something like a tuple to handle multiple return...

Arithmetic with Arbitrarily Large Integers in PHP

Ok, so PHP isn't the best language to be dealing with arbitrarily large integers in, considering that it only natively supports 32-bit signed integers. What I'm trying to do though is create a class that could represent an arbitrarily large binary number and be able to perform simple arithmetic operations on two of them (add/subtract/mul...

Populating a list of integers in .NET

I need a list of integers from 1 to x where x is set by the user. I could build it with a for loop eg assuming x is an integer set previously: List<int> iList = new List<int>(); for (int i = 1; i <= x; i++) { iList.Add(i); } This seems dumb, surely there's a more elegant way to do this, something like the PHP range method ...

Bit reversal of an integer, ignoring integer size and endianness

Given an integer typedef: typedef unsigned int TYPE; or typedef unsigned long TYPE; I have the following code to reverse the bits of an integer: TYPE max_bit= (TYPE)-1; void reverse_int_setup() { TYPE bits= (TYPE)max_bit; while (bits <<= 1) max_bit= bits; } TYPE reverse_int(TYPE arg) { TYPE bit_setter= 1,...

Simplest way to check if two integers have same sign?

Which is the simplest way to check if two integers have same sign? Is there any short bitwise trick to do this? ...

Why don't languages raise errors on integer overflow by default?

In several modern programming languages (including C++, Java, and C#), the language allows integer overflow to occur at runtime without raising any kind of error condition. For example, consider this (contrived) C# method, which does not account for the possibility of overflow/underflow. (For brevity, the method also doesn't handle the...

Best algorithm to count the number of set bits in a 32-bit integer?

8 bits representing the number 7 look like this: 00000111 Three bits are set. What is the best algorithm to determine the number of set bits in a 32-bit integer? ...

What does "0 but true" mean in Perl?

Can someone explain what exactly the string "0 but true" means in Perl? As far as I understand, it equals zero in an integer comparison, but evaluates to true when used as a boolean. Is this correct? Is this a normal behavior of the language or is this a special string treated as a special case in the interpreter? ...

How do I check if an integer is even or odd?

How best can I check if an integer is even or odd in C? I considered how I'd do this in Java, but I couldn't come up with an answer either. Thanks. ...

On 32-bit CPUs, is an 'integer' type more efficient than a 'short' type?

On a 32-bit CPU, an integer is 4 bytes and a short integer is 2 bytes. If I am writing a C/C++ application that uses many numeric values that will always fit within the provided range of a short integer, is it more efficient to use 4 byte integers or 2 byte integers? I have heard it suggested that 4 byte integers are more efficient as ...

What is the proper size for a sequence-generated primary key?

Currently, primary keys in our system are 10 digits longs, just over the limit for Java Integers. I want to avoid any maintenance problems down the road caused by numeric overflow in these keys, but at the same time I do not want to sacrifice much system performance to store infinitely large numbers that I will never need. How do you ha...

Integer formatting, padding to a given length

I need to pad the output of an integer to a given length. For example, with a length of 4 digits, the output of the integer 4 is "0004" instead of "4". How can I do this in C# 2.0? ...

Calling a C# web service from with PHP with a long parameter

We have a customer that is trying to call our web service written in C# from PHP code. The web service call takes a long as parameter. This call works fine for other customers calling from C# or Java but this customer is getting an error back from the call. I haven't debugged their specific call but I am guessing that the 64bit integer ...

What type for an integer of more than 4 bytes?

Hi, I have to use unsigned integers that could span to more than 4 bytes, what type should I use? PS Sorry for the "noobism" but that's it :D NB: I need integers because i have to do divisions and care only for the integer parts and this way int are useful ...

Convert a number to a string with specified length in C++

I have some numbers of different length (like 1, 999, 76492, so on) and I want to convert them all to strings with a common length (for example, if the length is 6, then those strings will be: '000001', '000999', '076492'). In other words, I need to add correct amount of leading zeros to the number. int n = 999; string str = some_func...

Convert Byte Array to Integer In VB.Net

I'm wonder what the best way to convert a byte array (length 4) to an integer is in vb.net? I'm aware of BitConverter, but it seems like quite a waste to do a function call to do something that should be able to be done by copying 4 bytes of memory. Along the same lines, what about converting a single/double from it's binary representat...

Alternative to itoa() for converting integer to string C++?

I was wonding if there was an alternative to itoa() for converting an integer to a string because when I run it in visual Studio I get warnings, and when I compile my program under Linux, it won't even compile. Thanks, tomek ...

Finding numerical substrings mathematically, without string comparison.

This originally was a problem I ran into at work, but is now something I'm just trying to solve for my own curiosity. I want to find out if int 'a' contains the int 'b' in the most efficient way possible. I wrote some code, but it seems no matter what I write, parsing it into a string and then using indexOf is twice as fast as doing it...