package Ġcom . ba eld ung . bit wise Ċ Ċ import Ġorg . junit . jupiter . api . Assertions . assertEquals Ċ import Ġorg . junit . jupiter . api . Test Ċ import Ġkotlin . test . assertTrue Ċ Ċ class ĠBit wise Operators UnitTest Ġ{ ĊĊĠĠĠ Ġ@ Test ĊĠĠĠ Ġfun Ġgiven _ two _ binaries _ when _ they _ are _ conj unct ed _ then _ only _ bits _ set _ in _ both _ surv ive () Ġ{ ĊĠĠĠĠĠĠĠ Ġval Ġa Ġ= Ġ 0 b 1 0 0 1 1 Ġ// Ġ 1 9 ĊĠĠĠĠĠĠĠ Ġval Ġb Ġ= Ġ 0 b 1 1 1 1 0 Ġ// Ġ 3 0 ĊĠĠĠĠĠĠĠ ĠassertEquals ( 0 b 1 0 0 1 0 , Ġa Ġand Ġb ) Ġ// Ġ 1 8 ĊĠĠĠ Ġ} ĊĊĠĠĠ Ġ@ Test ĊĠĠĠ Ġfun Ġgiven _ two _ binaries _ when _ they _ are _ dis j unct ed _ then _ bits _ from _ both _ appear _ in _ result () Ġ{ ĊĠĠĠĠĠĠĠ Ġval Ġa Ġ= Ġ 0 b 1 0 1 0 0 1 Ġ// Ġ 4 1 ĊĠĠĠĠĠĠĠ Ġval Ġb Ġ= Ġ 0 b 1 1 0 0 1 1 Ġ// Ġ 5 1 ĊĠĠĠĠĠĠĠ ĠassertEquals ( 0 b 1 1 1 0 1 1 , Ġa Ġor Ġb ) ĊĠĠĠ Ġ} ĊĊĠĠĠ Ġ@ Test ĊĠĠĠ Ġfun Ġgiven _ two _ binaries _ when _ they _ are _ ex lus ively _ dis j unct ed _ then _ only _ bits _ set _ differ ently _ surv ive () Ġ{ ĊĠĠĠĠĠĠĠ Ġval Ġa Ġ= Ġ 0 b 1 1 0 1 0 1 ĊĠĠĠĠĠĠĠ Ġval Ġb Ġ= Ġ 0 b 1 0 1 0 1 0 ĊĠĠĠĠĠĠĠ ĠassertEquals ( 3 1 , Ġa Ġxor Ġb ) ĊĠĠĠ Ġ} ĊĊĠĠĠ Ġ@ Test ĊĠĠĠ Ġfun Ġgiven _ a _ binary _ when _ it _ is _ inverted _ then _ all _ bits _ flip () Ġ{ ĊĠĠĠĠĠĠĠ ĠassertEquals ( 0 b 0 1 0 0 1 1 , Ġ 0 b 1 0 1 1 0 0 . inv () Ġand Ġ 0 b 1 1 1 1 1 1 ) ĊĠĠĠ Ġ} ĊĊĠĠĠ Ġ@ Test ĊĠĠĠ Ġfun Ġgiven _ a _ binary _ when _ it _ is _ shift ed _ left _ 2 _ bits _ then _ first _ two _ bits _ be come _ zero () Ġ{ ĊĠĠĠĠĠĠĠ ĠassertEquals ( 0 b 1 1 0 0 1 1 0 0 , Ġ 0 b 1 1 0 0 1 1 Ġshl Ġ 2 ) ĊĠĠĠ Ġ} ĊĊĊĠĠĠ Ġ@ Test ĊĠĠĠ Ġfun Ġgiven _ a _ negative _ binary _ when _ it _ is _ unsigned _ shift ed _ right _ then _ the _ sign _ will _ be _ lost () Ġ{ ĊĠĠĠĠĠĠĠ ĠassertEquals ( 0 b 1 1 1 1 1 1 1 1 1 1 , Ġ- 0 b 1 1 0 0 1 1 0 0 1 1 Ġ ush r Ġ 2 2 ) ĊĠĠĠ Ġ} ĊĊĠĠĠ Ġ@ Test ĊĠĠĠ Ġfun Ġgiven _ a _ negative _ binary _ when _ it _ is _ signed _ shift ed _ right _ then _ the _ sign _ will _ be _ pre served () Ġ{ ĊĠĠĠĠĠĠĠ ĠassertEquals ( 0 b 1 1 0 0 1 1 0 0 , Ġ 0 b 1 1 0 0 1 1 0 0 1 1 Ġshr Ġ 2 ) Ġ// Ġthe Ġfirst Ġ 2 2 Ġbytes Ġare Ġzero es ĊĠĠĠĠĠĠĠ Ġ// Ġthe Ġfirst Ġbit Ġis Ġ 1 , Ġit Ġmeans Ġthat Ġthe Ġnumber Ġis Ġnegative ĊĠĠĠĠĠĠĠ ĠassertEquals ( 0 b 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 0 0 1 1 0 0 1 1 , Ġ 0 b 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 0 0 1 1 0 0 1 1 0 1 Ġshr Ġ 2 ) ĊĠĠĠ Ġ} ĊĊĠĠĠ Ġ@ Test ĊĠĠĠ Ġfun Ġgiven _ an _ integer _ when _ shift ed _ right _ then _ it _ is _ div ided _ n _ times _ by _ two () Ġ{ ĊĠĠĠĠĠĠĠ ĠassertEquals ( 3 , Ġ 1 2 Ġshr Ġ 2 ) Ġ// Ġ 1 2 Ġ/ Ġ 2 ^ 2 Ġ== Ġ 1 2 Ġ/ Ġ 4 ĊĠĠĠ Ġ} ĊĊĠĠĠ Ġ@ Test ĊĠĠĠ Ġfun Ġgiven _ an _ integer _ when _ shift ed _ left _ then _ it _ is _ multiplied _ n _ times _ by _ two () Ġ{ ĊĠĠĠĠĠĠĠ ĠassertEquals ( 2 4 , Ġ 3 Ġshl Ġ 3 ) Ġ// Ġ 3 Ġ* Ġ 2 ^ 3 Ġ== Ġ 3 Ġ* Ġ 8 ĊĠĠĠ Ġ} ĊĊĠĠĠ Ġ@ Test ĊĠĠĠ Ġfun Ġgiven _ a _ mask _ and _ a _ bitmap _ when _ conj unct ed _ then _ specific _ bit _ is _ checked () Ġ{ ĊĠĠĠĠĠĠĠ Ġval ĠSK Y _ IS _ BLUE _ MASK Ġ= Ġ 0 b 0 0 0 0 0 0 0 0 0 0 0 0 0 1 0 0 0 0 0 0 0 0 0 0 0 0 ĊĊĠĠĠĠĠĠĠ Ġfun Ġis Sky Blue ( world Properties : ĠInt ): ĠBoolean Ġ= ĊĠĠĠĠĠĠĠĠĠĠĠ Ġworld Properties Ġand ĠSK Y _ IS _ BLUE _ MASK Ġ!= Ġ 0 ĊĊĠĠĠĠĠĠĠ ĠassertTrue ( is Sky Blue ( 0 b 1 0 0 1 1 1 0 0 1 1 1 1 0 1 0 1 1 1 0 1 0 1 0 1 0 1 )) ĊĠĠĠ Ġ} ĊĊĊĠĠĠ Ġ@ Test ĊĠĠĠ Ġfun Ġwhen _ dis j unct ed _ then _ set _ bits _ are _ unit ed () Ġ{ ĊĠĠĠĠĠĠĠ Ġval ĠSK Y _ IS _ BLUE Ġ= Ġ 0 b 0 0 0 0 0 0 0 0 0 0 0 0 0 1 0 0 0 0 0 0 0 0 0 0 0 0 ĊĠĠĠĠĠĠĠ Ġval ĠS UN _ IS _ SH IN ING Ġ= Ġ 0 b 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 0 0 0 0 0 0 0 0 0 0 0 ĊĠĠĠĠĠĠĠ Ġval Ġsky Is Blue And Sun Sh ines Ġ= ĠSK Y _ IS _ BLUE Ġor ĠS UN _ IS _ SH IN ING ĊĠĠĠĠĠĠĠ ĠassertEquals ( 0 b 0 0 0 0 0 0 0 0 0 0 0 0 0 1 1 0 0 0 0 0 0 0 0 0 0 0 , Ġsky Is Blue And Sun Sh ines ) ĊĠĠĠ Ġ} Ċ } Ċ