7
Binary multiplication 1001110 times 11001 0000000000001001110 0000000000000000000 0000000000000000000 0000000001001110000 0000000010011100000 create a table add the columns 0000000011013211110 simplify 0000000011110011110 #define Sj (strlen(B)-j-1) #define Si (strlen(A)-i-1) int main() { int i, T[1000]; char A[300], B[300]; while (cin >> A >> B) { for (i=0 ; i<1000 ; ++i) T[i] = 0; for (i=0 ; A[i] != 0 ; ++i) if (A[Si] == '1') for (int j=0 ; B[j] != 0 ; ++j) if (B[Sj] == '1') ++T[999-i-j]; /* add columns /* then simplify and print */ } } STL : http://www.sgi.com/Technology/STL

Binary multiplication

  • Upload
    lieu

  • View
    27

  • Download
    1

Embed Size (px)

DESCRIPTION

Binary multiplication. #define Sj (strlen(B)-j-1) #define Si (strlen(A)-i-1) int main() { int i, T[1000]; char A[300], B[300]; while (cin >> A >> B) { for (i=0 ; i

Citation preview

Page 1: Binary multiplication

Binary multiplication

1001110 times 11001

0000000000001001110

0000000000000000000

0000000000000000000

0000000001001110000

0000000010011100000

create a table

add the columns 0000000011013211110

simplify 0000000011110011110

#define Sj (strlen(B)-j-1)#define Si (strlen(A)-i-1)

int main() {

int i, T[1000]; char A[300], B[300];

while (cin >> A >> B) { for (i=0 ; i<1000 ; ++i) T[i] = 0;

for (i=0 ; A[i] != 0 ; ++i) if (A[Si] == '1') for (int j=0 ; B[j] != 0 ; ++j) if (B[Sj] == '1') ++T[999-i-j]; /* add columns */ /* then simplify and print */ } }

STL: http://www.sgi.com/Technology/STL

Page 2: Binary multiplication

Fraction calculator

getNum(…)

getOp(…)

operate(n1,d1,n2,d2,op)

simplify(int& n,int& d)

18 - --5 / 7 * 3 + 1 - 3 ...

getNum(…)

getOp(…)

18 -

evaluate as you go

15/7

when you see this, catch up with any

pending operations

111/7 +pending

pending

immediate

functions to help parse...

gets a number or returns an error

gets an operator or returns an error

does the arithmetic

reduces n/d to lowest terms

Page 3: Binary multiplication

C Output

printf, fprintf, sprintf(char* s, const char* format, …)the destination the format string the values

h.412-#0% d

start character

flags- left-justify0 pad w/ zeros+ use sign (+ or -)(space) use sign ( or -)# deviant operation

minimum field width

precision

size modifier

h shortl long (lowercase L)L long double

type

d decimal integersu unsigned (decimal) intso octal integersx hexadecimal integersf doubles (floats are cast)e doubles (exp. notation)g f or e, if exp < -3 or -4c characters stringn outputs # of chars written !!% two of these print a ‘%’

allowed size modifiers

possible format strings

Page 4: Binary multiplication

C Output

%10.4d

value = 42 value = -42

0042 -0042

%-#12x 0x2a 0xffffffd6

%+10.4g +42 -42.42

value = 42 value = -42.419

%- 10.4g 42 -42.42

%-#10.4g 42.00 -42.42

value = “forty-two”

%10.5s forty

Page 5: Binary multiplication

Stuff I’d never heard of...

sprintf(s,”%*.*f”,width,prec,number)

if width = 10, prec = 4, and number = 3.14159, then

What if you want to know how much was written to s ?

s == “ 3.142”

sprintf(s,”%*.*f %n”,width,prec,number,&n)

n == 11

after which

Page 6: Binary multiplication

Bugs & No Bugs

class Line

{

Point* p1;

Point* p2;

Line(Point* p1, Point* p2)

{

p1 = new Point();

p2 = new Point();

this.p1 = p1;

this.p2 = p2;

}

// other methods

}

#include <stdio.h>

char*T="IeJKLMaYQCE]jbZRskc[SldU^V\\X\\|/_<[<:90!\"$434-./2>]s",K[3][1000],*F,x,A,*M[2],*J,r[4],*g,N,Y,*Q,W,*k,q,D;X(){r [r[r[3]=M[1-(x&1)][*r=W,1],2]=*Q+2,1]=x+1+Y,*g++=((((x& 7)-1)>>1)-1)?*r:r[x>>3],(++x<*r)&&X();}E(){A||X(x=0,g =J),x=7&(*T>>A*3),J[(x[F]-W-x)^A*7]=Q[x&3]^A*(*M)[2 +(x&1)],g=J+((x[k]-W)^A*7)-A,g[1]=(*M)[*g=M[T+=A ,1][x&1],x&1],(A^=1)&&(E(),J+=W);}l(){E(--q&&l ());}B(){*J&&B((D=*J,Q[2]<D&&D<k[1]&&(*g++=1 ),!(D-W&&D-9&&D-10&&D-13)&&(!*r&&(*g++=0) ,*r=1)||64<D&&D<91&&(*r=0,*g++=D-63)||D >=97&&D<123&&(*r=0,*g++=D-95)||!(D-k[ 3])&&(*r=0,*g++=12)||D>k[3]&&D<=k[ 1]-1&&(*r=0,*g++=D-47),J++));}j( ){putchar(A);}b(){(j(A=(*K)[D* W+r[2]*Y+x]),++x<Y)&&b();}t (){(j((b(D=q[g],x=0),A=W) ),++q<(*(r+1)<Y?*(r+1): Y))&&t();}R(){(A=(t( q=0),'\n'),j(),++r [2]<N)&&R();}O() {(j((r[2]=0,R( ))),r[1]-=q) &&O(g-=-q) ;}C(){( J=gets (K[1]))&&C((B(g=K[2]),*r=!(!*r&&(*g++=0)),(*r)[r]=g-K[2],g=K[2],r[1]&&O()));;}main(){C((l((J=(A=0)[K],A[M]=(F=(k=(M[!A]=(Q=T+(q=(Y=(W=32)-(N=4))))+N)+2)+7)+7)),Y=N<<(*r=!-A)));;}

2000 IOCCC winner “Best use of flags”

www.cs.hmc.edu/~dodds/anderson.c

Page 7: Binary multiplication

“Power Towers”

Input: 20000 3

Output:

Idea:

n b

Write n in base b -- including all exponents!

20000 == 3 + 3 + 2*3 + 2*3 + 29 5 3 2

2

3 3+2 3 2

20000 = 3 +3 +2*3 +2*3 +2