bitwise division rounding to nearest zero - c

I want to perform an signed integer bitwise division by a power of 2. However, I encounter several problem. I just wonder if anyone can help me.
First, I try to use bit shifting alone:
int result = number >> n;
However, I got a problem when I try to divide a negative number. (it always round with a number with bigger magnitude. example: -9/4=-3 instead of -2. So, I look this problem in the internet, that end me up with this solution:
int result = (number + (1<<n)-1) >> n;
However, when I try 11/4 = 3 instead of 2
Any suggestions? I can only use ! ~ & ^ | + << >> (no loop or if/switch allowed)

The following method is bad because it relies on:
right shifts of negative integers being arithmetic shifts (may not be the case)
signed integers being in the 2's complement representation (extremely rarely may not be the case)
integers not having any padding bits (these days on modern CPUs you won't find padding bits, although the standard allows their existence)
And it may cause undefined behavior on some dividends (e.g. INT_MIN) due to signed integer overflow.
Therefore it isn't portable and isn't guaranteed to work always. You have been warned.
#include <stdio.h>
#include <limits.h>
int DivByShifting1(int n, unsigned shift)
{
int sgn = n >> ((sizeof(int) * CHAR_BIT) - 1);
return ((((n + sgn) ^ sgn) >> shift) + sgn) ^ sgn;
}
int main(void)
{
int n, s;
for (n = -10; n <= 10; n++)
for (s = 0; s <= 4; s++)
printf("%d / %d = %d\n", n, 1 << s, DivByShifting1(n, s));
return 0;
}
Output (ideone):
-10 / 1 = -10
-10 / 2 = -5
-10 / 4 = -2
-10 / 8 = -1
-10 / 16 = 0
-9 / 1 = -9
-9 / 2 = -4
-9 / 4 = -2
-9 / 8 = -1
-9 / 16 = 0
-8 / 1 = -8
-8 / 2 = -4
-8 / 4 = -2
-8 / 8 = -1
-8 / 16 = 0
-7 / 1 = -7
-7 / 2 = -3
-7 / 4 = -1
-7 / 8 = 0
-7 / 16 = 0
-6 / 1 = -6
-6 / 2 = -3
-6 / 4 = -1
-6 / 8 = 0
-6 / 16 = 0
-5 / 1 = -5
-5 / 2 = -2
-5 / 4 = -1
-5 / 8 = 0
-5 / 16 = 0
-4 / 1 = -4
-4 / 2 = -2
-4 / 4 = -1
-4 / 8 = 0
-4 / 16 = 0
-3 / 1 = -3
-3 / 2 = -1
-3 / 4 = 0
-3 / 8 = 0
-3 / 16 = 0
-2 / 1 = -2
-2 / 2 = -1
-2 / 4 = 0
-2 / 8 = 0
-2 / 16 = 0
-1 / 1 = -1
-1 / 2 = 0
-1 / 4 = 0
-1 / 8 = 0
-1 / 16 = 0
0 / 1 = 0
0 / 2 = 0
0 / 4 = 0
0 / 8 = 0
0 / 16 = 0
1 / 1 = 1
1 / 2 = 0
1 / 4 = 0
1 / 8 = 0
1 / 16 = 0
2 / 1 = 2
2 / 2 = 1
2 / 4 = 0
2 / 8 = 0
2 / 16 = 0
3 / 1 = 3
3 / 2 = 1
3 / 4 = 0
3 / 8 = 0
3 / 16 = 0
4 / 1 = 4
4 / 2 = 2
4 / 4 = 1
4 / 8 = 0
4 / 16 = 0
5 / 1 = 5
5 / 2 = 2
5 / 4 = 1
5 / 8 = 0
5 / 16 = 0
6 / 1 = 6
6 / 2 = 3
6 / 4 = 1
6 / 8 = 0
6 / 16 = 0
7 / 1 = 7
7 / 2 = 3
7 / 4 = 1
7 / 8 = 0
7 / 16 = 0
8 / 1 = 8
8 / 2 = 4
8 / 4 = 2
8 / 8 = 1
8 / 16 = 0
9 / 1 = 9
9 / 2 = 4
9 / 4 = 2
9 / 8 = 1
9 / 16 = 0
10 / 1 = 10
10 / 2 = 5
10 / 4 = 2
10 / 8 = 1
10 / 16 = 0
Note that ((sizeof(int) * CHAR_BIT) - 1) is a compile-time constant and therefore * and - can be allowed.
Another version, which is very similar, but does not require right shifts of negative integers to be arithmetic shifts and is free of signed integer overflow (2's complement-ness and padding bits are still limitations, but virtually in-existent in today's practice):
#include <stdio.h>
#include <limits.h>
#include <string.h>
int DivByShifting2(int n, unsigned shift)
{
unsigned un = n;
unsigned sgn = 1 + ~(un >> ((sizeof(int) * CHAR_BIT) - 1));
un = ((((un + sgn) ^ sgn) >> shift) + sgn) ^ sgn;
memcpy(&n, &un, sizeof n);
return n;
}
int main(void)
{
int n, s;
for (n = -10; n <= 10; n++)
for (s = 0; s <=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 4
8 / 4 = 2
8 / 8 = 1
8 / 16 = 0
9 / 1 = 9
9 / 2 = 4
9 / 4 = 2
9 / 8 = 1
9 / 16 = 0
10 / 1 = 10
10 / 2 = 5
10 / 4 = 2
10 / 8 = 1
10 / 16 = 0
#R.. rightfully reminds that the conversion from a signed int to an unsigned int can be done by adding 0u (unsigned 0).
And he also reminds that un can be returned directly instead of doing memcpy() to n. The conversion should be implementation-defined, but in 2's complement implementations of C, bit-for-bit copy is practically always the case.

Just use the / operator:
int result = number / (1 << n);
Any decent compiler will compile this to the optimal bitshift with fixup for "rounding" of negative results.

maybe this should help you.
1) if your using / operator then the standard says (ISO/IEC TR3 in 6.5.5 Multiplicative operators)that the result of the / operator is the quotient from the division of the first operand by the second.
2) if your using >> the standard says (ISO/IEC TR3 in 6.5.7) that the result of >> where the LHS operand is a signed type and negative then the resulting value is implementation defined.
so / will give you the result as you desired.
but >> on signed && negative number is dependant on your compiler.

Judging from the disassembly of int div(int a){return a/4;}:
leal 3(%rdi), %eax
testl %edi, %edi
cmovns %edi, %eax
sarl $2, %eax
one has to adjust the operand by (1<<n)-1 if and only if the operand is negative.
An unconditional method would be to use sgn(a)*(abs(a)>>n); both of which can be implemented with branchless bitmagic relying on the implementation defined behaviour.

Related

How to make C find all different symbol (+,-,*,/) combinations to have a certain output [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 4 years ago.
Improve this question
I have the following equation :
8 ? 7 ? 6 ? 5 ? 4 ? 3 ? 2 ? 1 = 36 and I need to make a C program that finds which operators ( from +, -, *, /) go instead of ? in order to make the equation true.
My initial thought is that I have 4*7=28 different combinations. So I started making arrays adding first all + then reduce the number of + by 1 and add another symbol and see if the equation is true. But I'm confused about the direction I took.
Also I haven't found anything similar in my many google searches, so this is my last resort.
Thanks!
What a nice puzzle...
Here is my solution (which became smaller as I myself expected).
findOps.c:
#include <assert.h>
#include <stdio.h>
/* too lazy to type this everytimes out... */
typedef unsigned int uint;
/* enumeration of all supported operators */
enum {
Add, Sub, Mul, Div
};
/* It is also used as encoding for compact storage with 2 bits. */
/* extracts operator i from ops. */
uint getOp(uint ops, uint i) { return (ops >> 2 * i) & 3; }
/* solves the equation with nValue values and (nValue - 1) ops
* and returns the result.
* This considers operator precedences appropriately.
*/
int solve(uint nValues, int values[], uint ops)
{
int sum = 0; /* accu for add, subtract */
int prod = values[0]; /* accu for multiply, divide */
for (int i = 1; i < nValues; ++i) {
int arg2 = values[i];
switch (getOp(ops, i - 1)) {
case Add:
sum += prod;
prod = arg2;
break;
case Sub:
sum += prod;
prod = -arg2;
break;
case Mul:
prod *= arg2;
break;
case Div:
prod /= arg2;
break;
}
}
sum += prod;
return sum;
}
/* pretty prints the equation out of internal representation. */
void print(uint nValues, int values[], uint ops, int result)
{
char chrOp[4] = { '+', '-', '*', '/' };
printf("%d", values[0]);
for (uint i = 1; i < nValues; ++i) {
printf(" %c %d", chrOp[getOp(ops, i - 1)], values[i]);
}
printf(" == %d\n", result);
}
/* main function */
int main()
{
/* assume some kind of input which provides the arguments and intended result */
int values[] = { 8, 7, 6, 5, 4, 3, 2, 1 };
enum { nValues = sizeof values / sizeof *values };
int result = 36;
/* check all combinations of operators */
uint opsEnd = 1 << 2 * (nValues - 1);
assert(8 * sizeof opsEnd >= 2 * (nValues - 1)); /* paranoid check whether opsEnd has enough bits */
uint ops = 0;
do {
if (solve(nValues, values, ops) == result) {
print(nValues, values, ops, result);
}
} while (++ops != opsEnd);
/* done */
return 0;
}
Test in cygwin on Windows 7:
$ gcc -std=c11 -o findOps findOps.c
$ ./findOps
8 + 7 + 6 + 5 + 4 + 3 + 2 + 1 == 36
8 * 7 - 6 * 5 + 4 + 3 + 2 + 1 == 36
8 * 7 - 6 - 5 * 4 + 3 + 2 + 1 == 36
8 + 7 * 6 - 5 * 4 + 3 + 2 + 1 == 36
8 * 7 - 6 - 5 - 4 * 3 + 2 + 1 == 36
8 + 7 * 6 - 5 - 4 * 3 + 2 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8 - 7 + 6 + 5 + 4 * 3 * 2 / 1 == 36
8 / 7 + 6 + 5 + 4 * 3 * 2 / 1 == 36
8 - 7 / 6 + 5 + 4 * 3 * 2 / 1 == 36
8 - 7 + 6 * 5 + 4 + 3 / 2 / 1 == 36
8 / 7 + 6 * 5 + 4 + 3 / 2 / 1 == 36
8 / 7 * 6 * 5 + 4 * 3 / 2 / 1 == 36
8 / 7 * 6 + 5 * 4 * 3 / 2 / 1 == 36
8 * 7 - 6 * 5 * 4 / 3 / 2 / 1 == 36
$
Live Demo on coliru
In ful trust on my coding abilities, I choosed randomly one line and checked it with the Windows calculator – it was correct.
Notes:
To iterate through all possible combinations of operators, the operators +, -, *, / are mapped to 0 ... 3. As these four values can be stored with exactly 2 bits, the sequence of all operators is stored in one unsigned. This makes iteration through all possible combinations extremely easy – it's just incrementing the resp. unsigned.
To solve the equation, I remembered how ancient pocket calculators (without support of ()) did this (with very limited resources). I didn't remember clearly (as it's decades ago somebody explained it to me) but was able to re-invent it. As there are only two possible precedences in +, -, *, /, it is fully sufficient to work with two buffers – one for the accumulated intermediate product, one for the accumulated intermediate sum.
The computations are done in int arithmetic. That means these computations are mathematical correct with the constraint to Natural numbers and integer operations. (I didn't check for overflow/underflow/wrap-around but I've a "good feeling" for the sample numbers. According to how solve() works, division by 0 cannot be an issue as long as there is no 0 in input.)
What I left out: parsing the text into the data structures I used in my sample. I leave this as exercise...

How can I extract a hex digit n from word x?

getHexDigit - Extract hex digit n from word x
Digits numbered from 0 (least significant) to 7 (most significant)
Examples: getHexDigit(0x12345678,2) = 0x6
Legal ops: ! ~ & ^ | + << >>
I'm confused because I don't understand how to extract and write only using those ops. Please help!!
int getHexDigit(int x, int n) {
return (x >> (n >> 4)) & 0xff; // How do I fix this
}
A single digit corresponds to 4 bits, such that you have to shift n*4 bits right; So you have to multiply n by 4, and since you must not use n * 4, you can simply write n << 2; shifting two bits left means 2*2:
return (x >> (n << 2)) & 0x0F;
Then you have to "unmask" all the digits except the one you want to have; Therefore the & 0x0F.
This should do it:
int getHexDigit(int x, int n) {
return (x >> (n << 2)) & 0xf;
}
First, multiply n by 4 (by shifting left by 2-bits) because each hex digit is 4 bits, and then just take that group out by shifting right and masking with 0x0f.
For example, see how it works for getHexDigit(0x1234, 2):
bit pos 1 1 1 1 1 1
5 4 3 2 1 0 9 8 7 6 5 4 3 2 1 0
(val) 1 2 3 4
x = 0 0 0 1 0 0 1 0 0 0 1 1 0 1 0 0
n = 2
n << 2 = 4
(val) 0 1 2 3
x >> 4 = 0 0 0 0 0 0 0 1 0 0 1 0 0 0 1 1
(val) 0 0 0 3
(x >> 4) & 0x0f = 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 1

Is this a bug in Arduino modulo or is it me?

I created the following simple sketch for my Arduino Due (running 1.6.1) using the modulo operator:
int count = 0;
void setup() {
Serial.begin(9600);
}
void loop() {
Serial.print("Count: ");
Serial.println(count);
Serial.print("Count / 4 = ");
Serial.println(count / 4);
Serial.print("Remainder = ");
Serial.println(count & 4);
Serial.println();
count++;
if (count == 50) {
delay(86400000);
} else {
delay(1000);
}
}
The output looks like this:
Count: 0
Count / 4 = 0
Remainder = 0
Count: 1
Count / 4 = 0
Remainder = 0
Count: 2
Count / 4 = 0
Remainder = 0
Count: 3
Count / 4 = 0
Remainder = 0
Count: 4
Count / 4 = 1
Remainder = 4
Count: 5
Count / 4 = 1
Remainder = 4
Count: 6
Count / 4 = 1
Remainder = 4
Count: 7
Count / 4 = 1
Remainder = 4
Count: 8
Count / 4 = 2
Remainder = 0
Count: 9
Count / 4 = 2
Remainder = 0
Count: 10
Count / 4 = 2
Remainder = 0
Count: 11
Count / 4 = 2
Remainder = 0
Count: 12
Count / 4 = 3
Remainder = 4
Count: 13
Count / 4 = 3
Remainder = 4
Count: 14
Count / 4 = 3
Remainder = 4
Count: 15
Count / 4 = 3
Remainder = 4
Count: 16
Count / 4 = 4
Remainder = 0
My expectation would be that the remainder value would count up from 0 to 3 again and again. Instead it alternates between 0 for four times and 4 for four times.
I'm completely open to the idea of me doing something wrong, but I can't figure out what it is.
I fail to see a modulo operator (%). I see & instead.

What algorithm maps number 27 to BBC

To clarify the confusion - I want to write a function that maps a number to the following list of letter combinations.
My question is best illustrated with the following table.
A 1
B 2
C 3
AA 4
AB 5
AC 6
BA 7
BB 8
BC 9
CA 10
CB 11
CC 12
AAA 13
AAB 14
AAC 15
ABA 16
ABB 17
ABC 18
ACA 19
ACB 20
ACC 21
BAA 22
BAB 23
BAC 24
BBA 25
BBB 26
BBC 27
I want to design a function that is able to map a given number, to the left column of this here table. I've tried assigning numerals to the letters first.
A = 0
B = 1
C = 2
This allows me form the following table (Cn - Column number, from right to left).
C3 C2 C1 Number
0 1
1 2
2 3
0 0 4
0 1 5
0 2 6
1 0 7
1 1 8
1 2 9
2 0 10
2 1 11
2 2 12
0 0 0 13
0 0 1 14
0 0 2 15
0 1 0 16
0 1 1 17
0 1 2 18
0 2 0 19
0 2 1 20
0 2 2 21
1 0 0 22
1 0 1 23
1 0 2 24
1 1 0 25
1 1 1 26
1 1 2 27
So this looks like an recursive loop type algorithm, but I can't figure out how to put this down in code. Any suggestions?
As whoever the person was (user: n.m.) who wrote the comment that disappeared, this is just base-3 counting, except all numerals are offset by +1. The digits really stand for A=0, B=1, C=2
Hence BBC = ('B'+1)*3^2 + ('B'+1)*3 + ('C'+1) = 2*9 + 2*3 + 3 = 27
The pseudocode for fromInt(), Antoine has already given you it. Same idea:
char* fromInt(int n) {
result = ""
working_val = (n-1)
while (working_val>0) {
Prepend to result the digit "CAB"[ working_val % 3 ]
working_val /= 3
}
return result
}
Strictly we don't care about catching the special-case 0 which Antoine noted, because your list doesn't have a representation for 0.
#include <stdio.h>
#include <string.h>
int toInt(const char *str, int acc) {
if(*str)
return toInt(++str, acc * 3 + *str - 'A' + 1);
else
return acc;
}
char *fromInt(int n, char *outbuff){
char* p = outbuff;
while(n){
*p++ = 'A' + ((n % 3 == 0)? 3 : n % 3) - 1;
n = (n - 1) / 3;
}
*p = '\0';
return strrev(outbuff);//strrev isn't ANSI C
}
int main(void) {
char buff[] = "BBC";
int x = toInt(buff, 0);
printf("%d\n", x);
printf("%s\n", fromInt(x, buff));
return 0;
}
This is a kind of base-3 system, but the digits are 1 (A), 2 (B) and 3 (C), there is no 0.
The conversion formula from this representation is, as usual,
3^n*a_n + 3^(n-1)*a_{n-1} + ... + 3^0*a_0
The reverse conversion is just like a regular conversion to base 3, the only difference is that a modified remainder function is used:
int modified_remainder(m, n)
{
int answer = m % n;
if (answer == 0) answer = n;
return answer;
}
Now given the number m, the last digit of its representation would be
a_0 = modified_remainder(m, 3)
The one before last is
m_1 = (m - a_0) / 3; // m-a_0 is always divisible by 3
a_1 = modified_remainder(m_1, 3)
The next one is
m_2 = (m_1 - a_1) / 3
a_2 = modified_remainder(m_2, 3)
and so on. You stop when m_k < n.
Try to verify these claims, it's a good exercise.

Deleting elements from array in Matlab

Given,
a = [2 4 6 8 10 0 7 18 9 0 8 2 0 5];
b = [1 3 0 5 70 8 6 87 1 9 7 8 0 2];
I am trying to delete elements (in both 'a' & 'b') that corresponds
to '0' or less than '0' in either 'a' or 'b' i.e., I want
% a = [2 4 8 10 7 18 9 8 2 5];
% b = [1 3 5 70 6 87 1 7 8 2];
I am trying like this -
n = length(b);
a1 = [];
b1 = [];
for k = 1:n
if a(n) <= 0 || b(n) <= 0
a1 = [a; a(a > 0)] % eliminates 0 from a
b1 = [b; b(b > 0)] % eliminates 0 from b
end
end
Any help will be very helpful.
Use find:
a = [2 4 6 8 10 0 7 18 9 0 8 2 0 5];
b = [1 3 0 5 70 8 6 87 1 9 7 8 0 2];
A = a( find( a > 0 & b > 0 ) );
B = b( find( a > 0 & b > 0 ) );
or even faster:
C = a( a > 0 & b > 0 );
D = b( a > 0 & b > 0 );
returns:
C =
2 4 8 10 7 18 9 8 2 5
D =
1 3 5 70 6 87 1 7 8 2
If you can be sure, that there are no values below zero you could also use:
E = a( logical(a) & logical(b) );
F = b( logical(a) & logical(b) );
which is a little faster, but containing also negative values.
The efficient and compact way to do this is to first create the relevant index, that prevents double calculation:
idx = a>0 & b>0
a = a(idx);
b = b(idx);

Resources