I need to implement the following function without branching or Boolean expressions: uint8_t func(uint32_t num, uint8_t shl) { if (num >= (1 << shl)) { return shl; } else { return 0; } } The first step I did was to notice that the else part is ea

This Wikipedia page mentions computation complexity of different mathematical operations, including addition, subtraction, multiplication and division. I'd like to focus on these four. First of all, every of the mentioned operations has its complexit

UVA Problem no. 10055, Hashmat the Brave Warrior, probably the easiest problem there. The input consists of a series of pairs of unsigned integers ≤ 2^32 (thus mandating the use of 64bit integers-) For each pair the task is to print out the differenc

I'm writing some software for a 32-bit cortex M0 microcontroller in C and I'm doing alot of manipulations with 32-bit RGB values. They are handled in a 32-bit integer format like 0x00BBRRGG. I want to be able to do math with them without worrying abo

This question already has an answer here: How to retrieve the first decimal digit of number efficiently 10 answers What's the fastest way to implement template <typename T> unsigned highest_decimal_digit(T x); (which returns e.g. 3 for 356431, 7 for

NOTE This is a theoretical question. I'm happy with the performance of my actual code as it is. I'm just curious about whether there is an alternative. Is there a trick to do an integer division of a constant value, which is itself an integer power o

What is bool guaranteed to return as? Can I rely on a bool type to always be interpreted as 0 or 1? If bool's numerical interpretation isn't reliable in C99 is there a standard where it is? Are there any things I should look out for in LLVM, GCC, or

I have the following statement. d = (pow(a,2*l+1)+1)/(val+1); Here, val, a and l are variables which are of no relation to the question. the numerator can exceed long long int range. denominator is a divisor of the numerator. But the final answer d w

Say i got two big numbers (defined below), and i want to implement division on them by falling back to x86 avaliable arithmetic 0008768376 - 1653656387 - 0437673667 - 0123767614 - 1039873878 - 2231712290 / 0038768167 - 3276287672 - 1665265628 C=A/B T

I have an expression in my code - int i = 10 + + 11 - - 12 + + 13 - - 14 + + 15; The value of the variable 'i' evaluates to 75, which is the sum of all the integers mentioned in the expression. How does the evaluation happen in this scenario?this eva

Integer i = new Integer(0); Integer j = new Integer(0); while(i <= j && j <= i && i !=j ){ System.out.println(i); } Why does this while loop execute? I understand that i != j. But separately, both i <= j and j <= i returns true

I have an int that represents numbers in the range [0, 8[ that wraps around: 2 1 3 0 4 7 5 6 Now I need to find the average of two numbers like this, so that for example the average of 7 and 0 would be 7.5, the average of 7 and 2 would be 0.5, the av

In order to get the exact sum of a long[] I'm using the following snippet. public static BigInteger sum(long[] a) { long low = 0; long high = 0; for (final long x : a) { low += (x & 0xFFFF_FFFFL); high += (x >> 32); } return BigInteger.valueOf(h

I have a array of bytes that are actually binary values like this byte[] buf = new byte[] { 0xF0, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF }; The binary representation is 1111 1110 (0xFF = 0, 0xF0= 1). The value of the integer or byte that needs to b

Before I get flamed, I want to say I do understand floating point numbers and things of the sort, but that doesn't seem to be my issue. To simplify things, I'm trying to determine if a number has more than 2 decimal places. I'm doing this by multiply

I decided it would be cool to learn Haskell by recreating arithmetic using only the Succ function, or other functions I've defined, and the Integer number type. So far I've managed to (re)create Add, Subtract, and Multiply, only for Integers. I've cr

I have two integers m and n, with m < n. In general, m doesn't divide n. Say that n = m*q + r, where q is the integer quotient of n and m, and r is the remainder (0 <= r < m). If we split n objects into m boxes as homogeneously as possible, r of

Java 8 is widely reported to have library support for unsigned integers. However, there seem to be no articles explaining how to use it and how much is possible. Some functions like Integer.CompareUnsigned are easy enough to find and seem to do what

I've recently embarked into Java programming and consider myself a programming novice. It seem that I'm having an issue with my source code arithmetic. I have verified all the nested if-else statements and they all work up to the final else statement

I need to do modulo 256 arithmetic in C. So can I simply do unsigned char i; i++; instead of int i; i=(i+1)%256; No. There is nothing that guarantees that unsigned char has eight bits. Use uint8_t from <stdint.h>, and you'll be perfectly fine. This

This is likely an easy solution that is simply eluding me. Specifically, I am creating locations on a canvas dynamically using a sin() function for equidistant points on a circle. Once these points are created, I am animating a shape moving from one

I want to get numbers entered from my Arduino keypad (which are of type 'char') converted to type 'int' so I can use them in variables and use math. For instance, if I press key "5", how do i put it into a variable "keyPressed" and do

Let a, b and c be non-large positive integers. Does a/b/c always equal a/(b * c) with C# integer arithmetic? For me, in C# it looks like: int a = 5126, b = 76, c = 14; int x1 = a / b / c; int x2 = a / (b * c); So my question is: does x1 == x2 for all

For the following code I get an overflow but sadly I cannot seem to understand why. std::int8_t smallValue = -1; unsigned int value = 500; std::uint8_t anotherSmallValue = 1; auto test = smallValue * value * anotherSmallValue; Afterwards test is a qu

What will be the regular expression in java for like this expression (3+2)+23/12-(43/54) in which left parentheses is create than user will be able to put right one and if left parentheses is not created than user will not able to put right parenthes

Have a look at the following piece of code: #include <stdio.h> int main(void) { int a; a = 2147483647; printf("a + 1 = %d \t sizeof (a + 1) = %lu\n", a + 1, sizeof (a + 1)); printf("a + 1L = %ld \t sizeof (a + 1L) = %lu\n", a + 1

int plus unsigned int returns an unsigned int. Should it be so? Consider this code: #include <boost/static_assert.hpp> #include <boost/typeof/typeof.hpp> #include <boost/type_traits/is_same.hpp> class test { static const int si = 0; stat

I have come across code from someone who appears to believe there is a problem subtracting an unsigned integer from another integer of the same type when the result would be negative. So that code like this would be incorrect even if it happens to wo

I have to store an integer value that is larger than the maximum value for the long datatype. How would I store and manipulate this value in memory? Please illustrate it through an example, if possible.Think about storing a numbers as sequences of de