Skip to main content
added the binary functionality and usage
Source Link
Syed
  • 59.6k
  • 5
  • 40
  • 95
Clear[CarryQ]
CarryQ[a_Integer, b_Integer] := 
 Length@IntegerDigits@(a  b) > Max@(Length@*IntegerDigits /@ {a , b})

SeedRandom[2];
list = RandomInteger[{20}, {5, 2}]~Join~RandomInteger[{1, 10}, {5, 2}]

{#1, #2, #1 #2, CarryQ[#1, #2]} & @@@ list //
 Grid[#, Alignment -> Left] &

enter image description here


For the binary case, you can make minor changes given that IntegerDigits has an argument for base and len. If you want to use 2s-complement arithmetic, then you can preferably add more context about the task that requires this calculation.

EDIT (for the binary case)

Let's introduce register length reg that I can arbitrarily set at 8 for the default case; although the function takes it as a parameter. If the sum can fit in the assigned register space then no carry is generated.

Clear[Base2CarryQ]
Base2CarryQ[a_Integer, b_Integer, reg_Integer : 8] := Module[
  {abin = IntegerDigits[a, 2, reg]
   , bbin = IntegerDigits[b, 2, reg]
   },
  Echo[abin];
  Echo[bbin];
  FromDigits[Echo@IntegerDigits[a b, 2, reg], 2] != a b
  ]

Usage: (remove Echo statements if not needed)

a = 2;
b = 128;
Base2CarryQ[a, b] 
(* carry out from the 
default 8 bit register *)
    
Base2CarryQ[a, b, 9] 
(* carry out? of a
 9-bit register *)

enter image description here


Clear[CarryQ]
CarryQ[a_Integer, b_Integer] := 
 Length@IntegerDigits@(a  b) > Max@(Length@*IntegerDigits /@ {a , b})

SeedRandom[2];
list = RandomInteger[{20}, {5, 2}]~Join~RandomInteger[{1, 10}, {5, 2}]

{#1, #2, #1 #2, CarryQ[#1, #2]} & @@@ list //
 Grid[#, Alignment -> Left] &

enter image description here


For the binary case, you can make minor changes given that IntegerDigits has an argument for base and len. If you want to use 2s-complement arithmetic, then you can preferably add more context about the task that requires this calculation.


Clear[CarryQ]
CarryQ[a_Integer, b_Integer] := 
 Length@IntegerDigits@(a  b) > Max@(Length@*IntegerDigits /@ {a , b})

SeedRandom[2];
list = RandomInteger[{20}, {5, 2}]~Join~RandomInteger[{1, 10}, {5, 2}]

{#1, #2, #1 #2, CarryQ[#1, #2]} & @@@ list //
 Grid[#, Alignment -> Left] &

enter image description here


For the binary case, you can make minor changes given that IntegerDigits has an argument for base and len. If you want to use 2s-complement arithmetic, then you can preferably add more context about the task that requires this calculation.

EDIT (for the binary case)

Let's introduce register length reg that I can arbitrarily set at 8 for the default case; although the function takes it as a parameter. If the sum can fit in the assigned register space then no carry is generated.

Clear[Base2CarryQ]
Base2CarryQ[a_Integer, b_Integer, reg_Integer : 8] := Module[
  {abin = IntegerDigits[a, 2, reg]
   , bbin = IntegerDigits[b, 2, reg]
   },
  Echo[abin];
  Echo[bbin];
  FromDigits[Echo@IntegerDigits[a b, 2, reg], 2] != a b
  ]

Usage: (remove Echo statements if not needed)

a = 2;
b = 128;
Base2CarryQ[a, b] 
(* carry out from the 
default 8 bit register *)
    
Base2CarryQ[a, b, 9] 
(* carry out? of a
 9-bit register *)

enter image description here


Source Link
Syed
  • 59.6k
  • 5
  • 40
  • 95

Clear[CarryQ]
CarryQ[a_Integer, b_Integer] := 
 Length@IntegerDigits@(a  b) > Max@(Length@*IntegerDigits /@ {a , b})

SeedRandom[2];
list = RandomInteger[{20}, {5, 2}]~Join~RandomInteger[{1, 10}, {5, 2}]

{#1, #2, #1 #2, CarryQ[#1, #2]} & @@@ list //
 Grid[#, Alignment -> Left] &

enter image description here


For the binary case, you can make minor changes given that IntegerDigits has an argument for base and len. If you want to use 2s-complement arithmetic, then you can preferably add more context about the task that requires this calculation.