Data commit
This commit is contained in:
parent
7387c8f97b
commit
cb5bb5e222
199093 changed files with 3378972 additions and 0 deletions
26
Task/Four-bit-adder/Scala/four-bit-adder-1.scala
Normal file
26
Task/Four-bit-adder/Scala/four-bit-adder-1.scala
Normal file
|
|
@ -0,0 +1,26 @@
|
|||
object FourBitAdder {
|
||||
type Nibble=(Boolean, Boolean, Boolean, Boolean)
|
||||
|
||||
def xor(a:Boolean, b:Boolean)=(!a)&&b || a&&(!b)
|
||||
|
||||
def halfAdder(a:Boolean, b:Boolean)={
|
||||
val s=xor(a,b)
|
||||
val c=a && b
|
||||
(s, c)
|
||||
}
|
||||
|
||||
def fullAdder(a:Boolean, b:Boolean, cIn:Boolean)={
|
||||
val (s1, c1)=halfAdder(a, cIn)
|
||||
val (s, c2)=halfAdder(s1, b)
|
||||
val cOut=c1 || c2
|
||||
(s, cOut)
|
||||
}
|
||||
|
||||
def fourBitAdder(a:Nibble, b:Nibble)={
|
||||
val (s0, c0)=fullAdder(a._4, b._4, false)
|
||||
val (s1, c1)=fullAdder(a._3, b._3, c0)
|
||||
val (s2, c2)=fullAdder(a._2, b._2, c1)
|
||||
val (s3, cOut)=fullAdder(a._1, b._1, c2)
|
||||
((s3, s2, s1, s0), cOut)
|
||||
}
|
||||
}
|
||||
15
Task/Four-bit-adder/Scala/four-bit-adder-2.scala
Normal file
15
Task/Four-bit-adder/Scala/four-bit-adder-2.scala
Normal file
|
|
@ -0,0 +1,15 @@
|
|||
object FourBitAdderTest {
|
||||
import FourBitAdder._
|
||||
def main(args: Array[String]): Unit = {
|
||||
println("%4s %4s %4s %2s".format("A","B","S","C"))
|
||||
for(a <- 0 to 15; b <- 0 to 15){
|
||||
val (s, cOut)=fourBitAdder(a,b)
|
||||
println("%4s + %4s = %4s %2d".format(nibbleToString(a),nibbleToString(b),nibbleToString(s),cOut.toInt))
|
||||
}
|
||||
}
|
||||
|
||||
implicit def toInt(b:Boolean):Int=if (b) 1 else 0
|
||||
implicit def intToBool(i:Int):Boolean=if (i==0) false else true
|
||||
implicit def intToNibble(i:Int):Nibble=((i>>>3)&1, (i>>>2)&1, (i>>>1)&1, i&1)
|
||||
def nibbleToString(n:Nibble):String="%d%d%d%d".format(n._1.toInt, n._2.toInt, n._3.toInt, n._4.toInt)
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue