8 bit unsigned number addition and subtraction overflow - math

I have trouble understanding when overflow occurs in unsinged addition and subtraction.
For example,
1 1 1 1 0 0 0 0
+ 0 0 1 1 1 0 0 0
__________________
0 0 1 0 1 0 0 0
Because of the 1 in the MSB, it results as overflow.
I understand this problem, but when it gets to subtraction, I have trouble determining when it is overflow
For example,
0 0 0 0 0 0 0 1
- 0 0 0 0 0 0 1 1
(After applying 2's complement)
0 0 0 0 0 0 0 1
+ 1 1 1 1 1 1 0 1
1 1 1 1 1 1 1 0
Therefore the result should be "no overflow" because there is no 1 carry out in the end. However, the answer says "overflow". Could you please tell me why?

I'd probably call this underflow not overflow.
Think about it like this. You are trying to compute x − y. Using two's complement you are implementing this as x + (2N − y) = 2N + (x − y). So the result will only correctly represent (x − y) if there is an overflow carry bit 2N that fell off the left. Otherwise the result you get is the two-s complement representation of a negative subtraction result.

Related

add the 8-bit signed magnitude binary numbers - binary arithmetic

I am trying to add 8-bit signed magnitude numbers: 10111000 + 00010111
0 1 1 0 0 0 0 0 carry
1 0 1 1 1 0 0 0 (-56)
+ 0 0 0 1 0 1 1 1 + (+23)
____________________ ________
1 0 0 1 1 1 1 != (-33)
I know that the most significant bit (MSB) is the sign bit and should not be added. However, I am not getting (-33) as the answer in binary, and there doesn't seem to be an overflow also. I am not sure if I am doing it the right way. Please help.
Addition in sign-magnitude representations is unfortunately not as straight-forward as with common complement representations. For sign-magnitude, you need to pick the correct operation based on the combination of signs. If you have equal signs, you simply add the magnitudes. If signs are unequal, you subtract the smaller from the larger and keep the sign of the larger magnitude. So, in your case:
0 0 0 1 1 1 0 borrow
0 1 1 1 0 0 0 (56)
- 0 0 1 0 1 1 1 - (23)
____________________ ________
0 1 0 0 0 0 1 == (33)
Taking the sign of the larger magnitude (56) gives you the final number 1 0 1 0 0 0 0 1.

"One-sided" Predictor Variable in Logistic Regression

Background Information:
I have 6 subjects; we'll call them A,B,C,D,E, and F.
Suppose they are asked to shoot basketballs from a free-throw line into a basket. A success is 1 and a failure is 0.
They performed as follows:
A - 0 0 1 0 0 1 1 1 0 0 1
B - 0 0 0 0 0 0 0 0 0 0 0
C - 1 0 1 1 0 0 0 0 1 0 0
D - 1 1 1 1 1 1 1 1 1 1 1
E - 1 1 0 0 0 0 0 1 1 0 0
F - 0 1 0 0 1 0 0 1 1 0 0
Question:
Now suppose I wanted to test that all of these subjects had the same probability of making a basket.
I would set up the logistic regression as such: Success being the probability of scoring a basket, and subject being the predictor variable.
Success ~ Subject.
Now this is where I get tangled up; I have one sided predictor variables, and what I mean by that is there is a subject that scored all of their baskets, and a subject that scored none of their's. How do we handle this type of logistic regression in r? Or can you suggest another method?
Thanks a ton!

Find all m-tuples that sum to n

I want to find ALL the non-negative integer solutions to the equation i+j+k+l+m=n where n is a non-negative integer. That is, I want to find all possible 5-tuples (i,j,k,l,m) with respect to a certain n, in R.
I wrote a code which is not working. I am suspicious there is something wrong in the looping.
For your convenience, I have taken n=3, so I am basically trying to compute all vectors (i,j,k,l,m) which are 35 in number, and the matrix a(35 by 5) is the matrix that is supposed to display those vectors. The whole thing is in the function "sample(n)", where if I put n=3 i.e. sample(3) when called will give me the matrix a. Please note that a (35 by 5) is defined beforehand with all entries 0.
sample=function(n){
i=0
j=0
k=0
l=0
m=0
for(p in 1:35){
while(i<=3){
while(j<=3){
while(k<=3){
while(l<=3){
m=n-(i+j+k+l)
if(m>-1){
a[p,]=c(i,j,k,l,m)
}
l=l+1}
k=k+1}
j=j+1}
i=i+1}
}
return(a)
}
When I call sample(3), I get my original a i.e. the matrix with all elements 0. What is wrong with this code? Please rectify it.
I don't think a brute-force approach will bring you much joy for this task. Instead you should look for existing functions that can be used and are efficient (i.e. implemented in C/C++).
n <- 3
library(partitions)
blockparts(rep(n, 5), n)
#[1,] 3 2 1 0 2 1 0 1 0 0 2 1 0 1 0 0 1 0 0 0 2 1 0 1 0 0 1 0 0 0 1 0 0 0 0
#[2,] 0 1 2 3 0 1 2 0 1 0 0 1 2 0 1 0 0 1 0 0 0 1 2 0 1 0 0 1 0 0 0 1 0 0 0
#[3,] 0 0 0 0 1 1 1 2 2 3 0 0 0 1 1 2 0 0 1 0 0 0 0 1 1 2 0 0 1 0 0 0 1 0 0
#[4,] 0 0 0 0 0 0 0 0 0 0 1 1 1 1 1 1 2 2 2 3 0 0 0 0 0 0 1 1 1 2 0 0 0 1 0
#[5,] 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 1 1 1 1 1 1 1 1 1 2 2 2 2 3
I believe that your code isn't answering your stated problem (as I understand it), on top of possible errors in your code.
One way to think of the problem is that, given the quadruple (i,j,k,l), the value of m = n - (i + j + k + l), while noting that the quadruple (i,j,k,l) is constrained so that n >= i+j+k+l AND i,j,k,l >= 0. For example, consider the following algorithm:
Let i freely take any value between 0 and n.
Given i, j can take values between 0 and n-i.
Given (i,j), k takes values between 0 and n-i-j.
Given (i,j,k), l takes values between 0 and n-i-j-k.
Given (i,j,k,l), m is defined as m = n - i - j - k -l.
The following code ought to answer your question. Please comment if this is not what you were looking for.
sample.example = function(n){
a=array(0,c(0,5))
for(i in 0:n){
for(j in seq(from=0,to=n-i,by=1)){
for(k in seq(from=0,to=n-i-j,by=1)){
for(l in seq(from=0,to=n-i-j-k,by=1)){
m = n - i -j - k - l
a = rbind(a,c(i,j,k,l,m))
}}}}
return(a)
}

Plot 3d bodies given as surfaces (, given as vertices sequences)

The title basically says it. I'd like to plot samples given in a form similar to the
cube below:
// Front
0 0 0
0 0 1
1 0 1
1 0 0
// Back
0 1 0
0 1 1
1 1 1
1 1 0
// Left
0 0 0
0 1 0
0 1 1
0 0 1
// Right
1 0 0
1 1 0
1 1 1
1 0 1
// Bottom
0 0 0
1 0 0
1 1 0
0 1 0
// Top
0 0 1
1 0 1
1 1 1
0 1 1
I'm not constrained to any particular program, but I assume gnuplot can be used to do it. How would you do it? (Of course I've tried to find it out myself, but I'm a gnuplot noob and gnuplot is too big to find answers directly)
I had to rearrange your datafile a little bit:
#front
0 0 0
0 0 1
1 0 0
1 0 1
#back
0 1 0
0 1 1
1 1 0
1 1 1
#left
0 0 0
0 1 0
0 0 1
0 1 1
#right
1 0 0
1 1 0
1 0 1
1 1 1
#bottom
0 0 0
1 0 0
0 1 0
1 1 0
#top
0 0 1
1 0 1
0 1 1
1 1 1
this can then be plotted with:
set pm3d depthorder
splot "datafile.dat" u 1:2:3:-2 w pm3d
While this works (mostly -- sorting by depthorder isn't perfect in gnuplot), it would quickly become tedious to do more complicated shapes.
Basically, it works because I specify each quadrilateral (square in this case) separately in the datafile -- quadrilaterals are separated by 2 blank lines. Each square is composed of the 4 vertices sorted in 2 "scans. So, the first scan you go from point (0,0,0) to (0,0,1). Then the next scan goes from (1,0,0) to (1,0,1). Gnuplot then connects the scans together.
Using this technique, you can also make triangles (triangles are just quadrilaterals where the last two points are the same ;).
Finally, the coloring of each surface is done by the pseudo-column "-2" -- which is really just the quadrilateral number. You could add a fourth field in the datafile and use that for the color if you prefer.
However, I have a feeling that this solution using gnuplot isn't really what you're looking for since this sort of thing isn't really what gnuplot was designed for -- There's probably a tool out there which would do this much more easily -- I just don't know what it is.

Binary data fusion

Can you please advise a way of binary data fusion?
Here is a a task:
There are n (n is odd) sources of binary labels (0 | 1). So, every data "frame" contains n labels. The task is to produce a single label per frame based on the fusion of all labels. For example:
S1 0 0 0 1 1 1 0 0 0 1 1 0
S2 0 0 1 1 1 1 1 0 0 1 1 1
S3 0 0 0 0 1 1 1 0 0 0 1 0
--------------------------
0 0 0 1 1 1 1 0 0 1 1 0
The "major voting" was used in this case: 0 0 0 -> 0; 1 1 0 -> 1 etc.
The major voting could be extended in horizontal direction, so that it's done over k frames for every i-th frame E.g. for k=3:
F1 round( (0+0+0+0+0+0+0+1+0) / 9) = 0
F2 round( (0+0+0+0+1+0+1+1+0) / 9) = 0
F3 round( (0+1+0+1+1+0+1+1+1) / 9) = 1 # was 0
F4 round( (1+1+0+1+1+1+1+1+1) / 9) = 1
..
Are there any other fusion schemes that come to your mind?
Thank you!
It looks to me you might be interested in
The tradeoff between reliability, consistency and availability. Here you can read about it with Amazon's Dynamo as an example.
Forward Error Correction

Resources