Convert int to ASCII and back in Python

I’m working on making a URL shortener for my site, and my current plan (I’m open to suggestions) is to use a node ID to generate the shortened URL. So, in theory, node 26 might be short.com/z, node 1 might be short.com/a, node 52 might be short.com/Z, and node 104 might be short.com/ZZ. When a … Read more

How to convert integers to characters in C?

In C, int, char, long, etc. are all integers. They typically have different memory sizes and thus different ranges as in INT_MIN to INT_MAX. char and arrays of char are often used to store characters and strings. Integers are stored in many types: int being the most popular for a balance of speed, size and … Read more

Is there any difference between 1U and 1 in C?

On most compliers, both will give a result with the same representation. However, according to the C specification, the result of a bit shift operation on a signed argument gives implementation-defined results, so in theory 1U << i is more portable than 1 << i. In practice all C compilers you’ll ever encounter treat signed left shifts the same … Read more

How does Python manage int and long?

int and long were “unified” a few versions back. Before that it was possible to overflow an int through math ops. 3.x has further advanced this by eliminating long altogether and only having int. Python 2: sys.maxint contains the maximum value a Python int can hold. On a 64-bit Python 2.7, the size is 24 bytes. Check with sys.getsizeof(). Python 3: sys.maxsize contains the … Read more

Java reverse an int value without using array

I am not clear about your Odd number. The way this code works is (it is not a Java specific algorithm) Eg. input =2345 first time in the while loop rev=5 input=234 second time rev=5*10+4=54 input=23 third time rev=54*10+3 input=2 fourth time rev=543*10+2 input=0 So the reversed number is 5432. If you just want only … Read more

How to convert integer to string in C?

Use sprintf(): All numbers that are representable by int will fit in a 12-char-array without overflow, unless your compiler is somehow using more than 32-bits for int. When using numbers with greater bitsize, e.g. long with most 64-bit compilers, you need to increase the array size—at least 21 characters for 64-bit types.

Declaring an unsigned int in Java

Java does not have a datatype for unsigned integers. You can define a long instead of an int if you need to store large values. You can also use a signed integer as if it were unsigned. The benefit of two’s complement representation is that most operations (such as addition, subtraction, multiplication, and left shift) are identical on a binary level for … Read more

Signed versus Unsigned Integers

Am I correct to say the difference between a signed and unsigned integer is: Unsigned can hold a larger positive value and no negative value. Unsigned uses the leading bit as a part of the value, while the signed version uses the left-most-bit to identify if the number is positive or negative. Signed integers can … Read more