@@ -231,8 +231,7 @@ public static string __format__(CodeContext/*!*/ context, int self, [NotDynamicN
231231 // So use FormattingHelper.ToCultureString for that support.
232232 if ( spec . Fill . HasValue && spec . Fill . Value == '0' && width > 1 ) {
233233 digits = FormattingHelper . ToCultureString ( self , culture . NumberFormat , spec ) ;
234- }
235- else {
234+ } else {
236235 digits = self . ToString ( "N0" , culture ) ;
237236 }
238237 break ;
@@ -246,8 +245,7 @@ public static string __format__(CodeContext/*!*/ context, int self, [NotDynamicN
246245 // so use FormattingHelper.ToCultureString for that support.
247246 if ( spec . Fill . HasValue && spec . Fill . Value == '0' && width > 1 ) {
248247 digits = FormattingHelper . ToCultureString ( self , FormattingHelper . InvariantCommaNumberInfo , spec ) ;
249- }
250- else {
248+ } else {
251249 digits = self . ToString ( "#,0" , CultureInfo . InvariantCulture ) ;
252250 }
253251 } else {
@@ -299,8 +297,7 @@ public static string __format__(CodeContext/*!*/ context, int self, [NotDynamicN
299297 } else if ( spec . ThousandsComma ) {
300298 // Handle the common case in 'd'.
301299 goto case 'd' ;
302- }
303- else {
300+ } else {
304301 digits = self . ToString ( CultureInfo . InvariantCulture ) ;
305302 }
306303 break ;
@@ -391,7 +388,7 @@ internal static string ToBinary(int self) {
391388 if ( self == Int32 . MinValue ) {
392389 return "-0b10000000000000000000000000000000" ;
393390 }
394-
391+
395392 string res = ToBinary ( self , true ) ;
396393 if ( self < 0 ) {
397394 res = "-" + res ;
@@ -422,14 +419,155 @@ private static string ToBinary(int self, bool includeType) {
422419 } else {
423420 digits = "10000000000000000000000000000000" ;
424421 }
425-
422+
426423 if ( includeType ) {
427424 digits = "0b" + digits ;
428425 }
429426 return digits ;
430427 }
431428
432429 #endregion
430+
431+ #region Mimic BigInteger members
432+ // Ideally only on instances
433+
434+ #region Properties
435+
436+ [ SpecialName , PropertyMethod , PythonHidden ]
437+ public static bool GetIsEven ( int self ) => ( self & 1 ) == 0 ;
438+
439+ [ SpecialName , PropertyMethod , PythonHidden ]
440+ public static bool GetIsOne ( int self ) => self == 1 ;
441+
442+ [ SpecialName , PropertyMethod , PythonHidden ]
443+ public static bool GetIsPowerOfTwo ( int self ) => self > 0 && ( self & ( self - 1 ) ) == 0 ;
444+
445+ [ SpecialName , PropertyMethod , PythonHidden ]
446+ public static bool GetIsZero ( int self ) => self == 0 ;
447+
448+ [ SpecialName , PropertyMethod , PythonHidden ]
449+ public static int GetSign ( int self ) => self == 0 ? 0 : self > 0 ? 1 : - 1 ;
450+
451+ [ PythonHidden ]
452+ public static object Zero => ScriptingRuntimeHelpers . Int32ToObject ( 0 ) ;
453+
454+ [ PythonHidden ]
455+ public static object One => ScriptingRuntimeHelpers . Int32ToObject ( 1 ) ;
456+
457+ [ PythonHidden ]
458+ public static object MinusOne => ScriptingRuntimeHelpers . Int32ToObject ( - 1 ) ;
459+
460+ #endregion
461+
462+ #region Methods
463+
464+ [ PythonHidden ]
465+ public static byte [ ] ToByteArray ( int self ) => new BigInteger ( self ) . ToByteArray ( ) ;
466+
467+ #if NETCOREAPP
468+ [ PythonHidden ]
469+ public static byte [ ] ToByteArray ( int self , bool isUnsigned = false , bool isBigEndian = false ) => new BigInteger ( self ) . ToByteArray ( isUnsigned , isBigEndian ) ;
470+
471+ [ PythonHidden ]
472+ public static int GetByteCount ( int self , bool isUnsigned = false ) => new BigInteger ( self ) . GetByteCount ( isUnsigned ) ;
473+
474+ [ PythonHidden ]
475+ public static bool TryWriteBytes ( int self , Span < byte > destination , out int bytesWritten , bool isUnsigned = false , bool isBigEndian = false )
476+ => new BigInteger ( self ) . TryWriteBytes ( destination , out bytesWritten , isUnsigned , isBigEndian ) ;
477+
478+ #elif NETSTANDARD
479+ [ PythonHidden ]
480+ public static byte [ ] ToByteArray ( int self , bool isUnsigned = false , bool isBigEndian = false ) {
481+ if ( self < 0 && isUnsigned ) throw new OverflowException ( "Negative values do not have an unsigned representation." ) ;
482+ byte [ ] bytes = ToByteArray ( self ) ;
483+ int count = bytes . Length ;
484+ if ( isUnsigned && count > 1 && bytes [ count - 1 ] == 0 ) Array . Resize ( ref bytes , count - 1 ) ;
485+ if ( isBigEndian ) Array . Reverse ( bytes ) ;
486+ return bytes ;
487+ }
488+
489+ [ PythonHidden ]
490+ public static int GetByteCount ( int self , bool isUnsigned = false ) => ToByteArray ( self , isUnsigned ) . Length ;
491+
492+ [ PythonHidden ]
493+ public static bool TryWriteBytes ( int self , Span < byte > destination , out int bytesWritten , bool isUnsigned = false , bool isBigEndian = false ) {
494+ bytesWritten = 0 ;
495+ byte [ ] bytes = ToByteArray ( self , isUnsigned , isBigEndian ) ;
496+ if ( bytes . Length > destination . Length ) return false ;
497+
498+ bytes . AsSpan ( ) . CopyTo ( destination ) ;
499+ bytesWritten = bytes . Length ;
500+ return true ;
501+ }
502+ #endif
503+
504+ #if NET || NETSTANDARD
505+ [ PythonHidden ]
506+ public static long GetBitLength ( int self ) {
507+ int length = MathUtils . BitLength ( self ) ;
508+ if ( self < 0 && ( self == int . MinValue || GetIsPowerOfTwo ( - self ) ) ) length -- ;
509+ return length ;
510+ }
511+ #endif
512+
513+ #endregion
514+
515+ #region Static Extension Methods
516+
517+ [ StaticExtensionMethod , PythonHidden ]
518+ public static BigInteger Compare ( BigInteger left , BigInteger right ) => BigInteger . Compare ( left , right ) ;
519+
520+ [ StaticExtensionMethod , PythonHidden ]
521+ public static BigInteger Min ( BigInteger left , BigInteger right ) => BigInteger . Min ( left , right ) ;
522+
523+ [ StaticExtensionMethod , PythonHidden ]
524+ public static BigInteger Max ( BigInteger left , BigInteger right ) => BigInteger . Max ( left , right ) ;
525+
526+ [ StaticExtensionMethod , PythonHidden ]
527+ public static double Log ( BigInteger value ) => BigInteger . Log ( value ) ;
528+
529+ [ StaticExtensionMethod , PythonHidden ]
530+ public static double Log ( BigInteger value , double baseValue ) => BigInteger . Log ( value , baseValue ) ;
531+
532+ [ StaticExtensionMethod , PythonHidden ]
533+ public static double Log10 ( BigInteger value ) => BigInteger . Log10 ( value ) ;
534+
535+ [ StaticExtensionMethod , PythonHidden ]
536+ public static object Pow ( BigInteger value , int exponent ) => BigInteger . Pow ( value , exponent ) ;
537+
538+ [ StaticExtensionMethod , PythonHidden ]
539+ public static object ModPow ( BigInteger value , BigInteger exponent , BigInteger modulus ) => BigInteger . ModPow ( value , exponent , modulus ) ;
540+
541+ [ StaticExtensionMethod , PythonHidden ]
542+ public static BigInteger Negate ( BigInteger value ) => BigInteger . Negate ( value ) ;
543+
544+ [ StaticExtensionMethod , PythonHidden ]
545+ public static BigInteger Abs ( BigInteger value ) => BigInteger . Abs ( value ) ;
546+
547+ [ StaticExtensionMethod , PythonHidden ]
548+ public static BigInteger Add ( BigInteger left , BigInteger right ) => BigInteger . Add ( left , right ) ;
549+
550+ [ StaticExtensionMethod , PythonHidden ]
551+ public static BigInteger Subtract ( BigInteger left , BigInteger right ) => BigInteger . Subtract ( left , right ) ;
552+
553+ [ StaticExtensionMethod , PythonHidden ]
554+ public static BigInteger Multiply ( BigInteger left , BigInteger right ) => BigInteger . Multiply ( left , right ) ;
555+
556+ [ StaticExtensionMethod , PythonHidden ]
557+ public static BigInteger Divide ( BigInteger left , BigInteger right ) => BigInteger . Divide ( left , right ) ;
558+
559+ [ StaticExtensionMethod , PythonHidden ]
560+ public static BigInteger Remainder ( BigInteger dividend , BigInteger divisor ) => BigInteger . Remainder ( dividend , divisor ) ;
561+
562+ [ StaticExtensionMethod , PythonHidden ]
563+ public static BigInteger DivRem ( BigInteger dividend , BigInteger divisor , out BigInteger remainder ) => BigInteger . DivRem ( dividend , divisor , out remainder ) ;
564+
565+ [ StaticExtensionMethod , PythonHidden ]
566+ public static BigInteger GreatestCommonDivisor ( BigInteger left , BigInteger right ) => BigInteger . GreatestCommonDivisor ( left , right ) ;
567+
568+ #endregion
569+
570+ #endregion
433571 }
434572
435573 public static partial class Int64Ops {
0 commit comments