arithmetic

Arithmetic Overflow

Why is it that an arithmetic overflow cannot occur when adding an positive and a negative number using two's complement. If you could please provide an example with 8-bit signed integers (bytes). ...

64 bit by 32 bit division

I am looking for a fast way to perform the following divison: Dividend is a signed 64 bit integer. Divisor is a signed 32 bit integer. Quotient should be a signed 64 bit integer, remainder is unnecessary. Low dword of the dividend is zero. I am using only 32 bit data types, since 64 bit ones are poorly supported by the compiler, and ...

Pointer arithmetic for void pointer in C

If a particular type(say int,char,float,..) pointer is incremented the value of pointer variable increased by number which is equal to size of the particular data type.If a void pointer points to data of x size for increment operation how it will point to x size ahead? How compiler knows to add x to value of the pointer? ...

How do you "concatenate" two 32 bits int to get a 64 bits long in Python?

I want to generate 64 bits long int to serve as unique ID's for documents. One idea is to combine the user's ID, which is a 32 bit int, with the Unix timestamp, which is another 32 bits int, to form an unique 64 bits long integer. A scaled-down example would be: Combine two 4-bit numbers 0010 and 0101 to form the 8-bit number 00100101...

Pointer addition vs subtraction

$5.7 - "[..]For addition, either both operands shall have arithmetic or enumeration type, or one operand shall be a pointer to a completely defined object type and the other shall have integral or enumeration type. 2 For subtraction, one of the following shall hold: — both operands have arithmetic or enumeration type; or — both operand...

What is the behavior of integer division in C?

i.e. - int result; result = 125/100; or result = 43/100; Will result always be the floor of the division? What is the defined bahavior? If you could point me in the right direction, i would appreciate it. Thank You. ...

How does modulus of a smaller dividend and larger divisor work?

7 % 3 = 1 (remainder 1) how does 3 % 7 (remainder ?) work? ...

what can you do with just binary or ternary raster operations ?

What soft of bit twiddling can and can't you do using binary or ternary raster arithmetic with blitting functions. Some more examples of bit twiddling: avoiding branches in chess programming, hacker's delight ...

Grammar with Parenthesis

I have this grammar and need to modify it to allow parenthesis like: (-1) and -(1*5) possibly 1+(2*5) as well as unary the unary minus sign. Does anyone have any suggestions of how to do so? <expr> ::= <term> | <expr> <op1> <term> <term> ::= <darg> |<term> <op2> <darg> <darg> ::= <digit> | <darg> <digit> <digit> ::= 0 | 1 | 2 | 3 |...

Numpy Modular arithmetic

Hi, How can I define in numpy a matrix that uses operations modulo 2? For example: 0 0 1 0 1 0 1 1 + 0 1 = 1 0 Thanks! ...

Real number arithmetic in a general purpose language?

As (hopefully) most of you know, floating point arithmetic is different from real number arithmetic. It's for starters imprecise. Many numbers, especially decimals (0.1, 0.3) cannot be represented, leading to problems like this. A more thorough list can be found here. Are there any general purpose languages that have built-in support fo...

Count number of chars in char array including spaces until null char

I'm trying to count the number of chars in a char array including the space until the end of the string. The following compiles but doesn't return the correct value, I'm trying to use pointer arithmetic to interate through my array. int numberOfCharsInArray(char* array) { int numberOfChars = 0; while (array++ != '\0')...

Why is `print (52-80)*42` different than `print 42*(52-80)` in Perl?

PERL:: What is: (52-80)*42 42*(52-80)? Ans: 1) -28 2) -1176. Why? Have fun explaining/justifying this please! #!/usr/bin/perl use strict; print 42*(52-80) , "\n"; print ((52-80)*42) , "\n"; print (52-80)*42 , "\n"; print "\n"; my $i=(52-80)*42; print $i, "\n"; Output: > -1176 > -1176-28 > -1176 ...

Strange conversion from NSUInteger to floats.

I have the following code: NSUInteger one = 1; CGPoint p = CGPointMake(-one, -one); NSLog(@"%@", NSStringFromCGPoint(p)); Its output: {4.29497e+09, 4.29497e+09} On the other hand: NSUInteger one = 1; NSLog(@"%i", -one); // prints -1 I know there’s probably some kind of overflow going on, but why do the two cases differ and why d...

How to parse arithmetic operators from string?

Is it possible to assign an int variable a value that is a result of expression written in a string? E.g. I have a string "5 - 3" and the expected result is 2. ...

Complement of a number

I read on wikipedia : "In general, for a radix r's complement encoding, with r the base (radix) of the number system, an integer part of m digits and fractional part of n digits, then the r's complement of a number 0 ≤ N < r^(m−1)−r^(−n) is determined by the formula: N** = (r^m − N) mod (r^m) " I don't understand that does the number...

Conversion of fixed point signed Q8 to Q4 format

Hi I need to convert from fixed point signed Q8 format to fixed point signed Q4 format in c. I assume that I can just do a bitshift by four, is this correct? Do I need to take into account the sign bit? Update: This is for an ARM11 architecture, but I would prefer a non-architecture specific solution. Thanks ...

In Perl, how can I do 64bit hex/decimal arithmetic AND output full number in HEX as string?

I need to do some arithmetic with large hexadecimal numbers below, but when I try to output I'm getting overflow error messages "Hexadecimal number > 0xffffffff non-portable", messages about not portable, or the maximum 32bit hex value FFFFFFFF. All of which imply that the standard language and output routines only cope with 32 bit value...

Python - question about decimal arithmetic

I have 3 questions pertaining to decimal arithmetic in Python, all 3 of which are best asked inline: 1) >>> from decimal import getcontext, Decimal >>> getcontext().prec = 6 >>> Decimal('50.567898491579878') * 1 Decimal('50.5679') >>> # How is this a precision of 6? If the decimal counts whole numbers as >>> # part of the precision, is...

I'm having trouble performing arithmetic expressions in UNIX.

I have the following script: #!/bin/sh r=3 r=$((r+5)) echo r However, I get this error: Syntax error at line 3: $ unexpected. I don't understand what I'm doing wrong. I'm following this online guide to the letter http://www.unixtutorial.org/2008/06/arithmetic-operations-in-unix-scripts/ ...