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

Is it possible to create a predicate max/2 without an accumulator so that max(List, Max) is true if and only if Max is the maximum value of List (a list of integers)?Yes, you can calculate the maximum after the recursive step. Like: max([M],M). % the

I see that the below code: byte b = 127; b = b + 1; b = b + 1; System.out.println(b); leads to boundary checking during runtime, and the first addition takes b out of range, leading to following error - Main.java:11: error: incompatible types: possib

This would be the general problem statement: A prisoner escapes from the jail by jumping over N walls each with height of each wall given in an array. He can jump x meters of height, but after every jump he slips y meters due to some uncontrollable f

I need to implement an algorithm to drive a Car object. This specific type of car consumes fuel at a higher rate for a while at the beginning of the journey, and at a lower rate from thereon. I'm struggling to implement the algorithm using only integ

I am doing review questions which ask me "What is the output of the following," and I am having some trouble understanding something about this function: int a = 1, b = 1, c = -1; c = --a && b++; printf("%d %d %d", a, b, c); Th

Last week I was in an interview and there was a test like this: Calculate N/9 (given that N is a positive integer), using only SHIFT LEFT, SHIFT RIGHT, ADD, SUBSTRACT instructions.first, find the representation of 1/9 in binary 0,0001110001110001 mea

I'm undertaking an exercise that hinges on being able to do this task. I have to take a multiple digit integer >=0, i.e. 830124, and make an array of the individual digits. My line of thinking is that I can convert it to a string, index the string in

I am a novice in prolog programming, i use swi-prolog. Now I'm stucked by some math problems as we know the predicate :A is 3+3.works well,the answer is A=6. but if I want to find two digits (A and B) from 0~9 that a+b=6 6 is A+B does't work. so I wa

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 am debugging a production code written in C and its simplest form can be shown as - void test_fun(int sr) { int hr = 0; #define ME 65535 #define SE 256 sr = sr/SE; <-- This should yield 0 if(sr == 1) hr = ME; else hr = (ME+1)/sr; <-- We should cra

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

I want to implement my own (simple) large/arbitrary integer precision arithmetic, first in Java (cause I am more familiar with the syntax), then rewrite it to C. I have addition, subtraction and multiplication for numbers of infinite length and now I

First, this is a homework assignment. I am supposed to create a program using a switch command. It asks the user to input 3 integers, then input an integer of 1-5 for the five cases average, max, min, total, and exit. I have pretty much completed thi

Given a non-negative integer c, I need an efficient algorithm to find the largest integer x such that x*(x-1)/2 <= c Equivalently, I need an efficient and reliably accurate algorithm to compute: x = floor((1 + sqrt(1 + 8*c))/2) (1) For the sake of de

OpenBSD's C library has an extension called reallocarray(3) which does realloc(array, size*nmemb) without blowing up if the multiplication overflows. The implementation contains this fragment: /* * This is sqrt(SIZE_MAX+1), as s1*s2 <= SIZE_MAX * if

C language has signed and unsigned types like char and int. I am not sure, how it is implemented on assembly level, for example it seems to me that multiplication of signed and unsigned would bring different results, so do assembly do both unsigned a

I am reading a 24-bit value, presently into a uint32_t variable. After bashing my head against the wall calculating the difference between two such variables, and thinking "it's unsigned! Why is this overflow a problem?!", I realised the values

I need to write program to find the integer square root of a number which is thousands of digits long. I can't use Newton Raphson as I don't have data types to store and divide such large numbers. I am using a long array in C to store the number. Is

In c ~ is 1's complement operator. This is equivalent to: ~a = -b + 1 So, a - ~b -1 = a-(-b + 1) + 1 = a + b – 1 + 1 = a + b Can anyone explains this to me?From elementary school math we know a = -(-a); From twos complement we know that -a = (~a) + 1

I'm trying to multiply two 128-bit integers in C. Here is my algorithm: Split the two 128-bit sequences into S1 and S2. Then split S1 into S11 (front/higher half) and S12 (back/lower half) and split S2 into S21 (front/higher half) and S22 (back/lower

This question already has an answer here: Is floating point math broken? 22 answers I tried this expression in both my FireFox and Chrome console: 17.99 * 100; Expected Result: 1799 Actual Result: 1798.9999999999998 I also tried: parseInt(17.99 * 100

I was a little discouraged such a huge difference in performance when working on 32-bit machines with 32-bit integers in Dart language. Is this means that Dart VM still not optimized for integer arithmetic? Here is my pretty straightforward test. voi

Im having trouble extracting the String data from a stack and converting it to int to perform arithmetic, it keeps telling me that i cannot cast a string to an int and vice versa, however i don't know any other alternative methods. this is the code i

I am reading this in a Lisp textbook: Lisp can perform some amazing feats with numbers, especially when compared with most other languages. For instance, here we're using the function expt to calculate the fifty-third power of 53: CL> (expt 53 53) 24

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

Previously today I was trying to add two ushorts and I noticed that I had to cast the result back to ushort. I thought it might've become an uint (to prevent a possible unintended overflow?), but to my surprise it was an int (System.Int32). Is there

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