Complément à deux en swift dans le sens inverse



        import Foundation
        import Glibc
        
        
        
        let decimal : UInt8 = 0b00001011
       
        
        print(decimal)
        //11 en binaire =      0000 1011
        // we reverse the bits 1111 0100
        // we add 1 :       1111 0101
        //      the binary code :  2^7 + 2^6 + 2^5 +2^4 +2^2 + 2^0 = 245
        
        let random = Int((~UInt8(decimal) + 1)) 
        // UInt8 converted the integer as binary: 1111 0100
        // operator ~ reverse bits
        // and add 1 to the last bit of the binary code: 1111 0101
        
        print(random)
        //245 
        
        
        
        let decimal1 = 245
        
        print(decimal1)
        //245
        
        let random1 = Int((~UInt8(decimal1) + 1)) 
        //245 in binary :    1111 0101
        //we reverse the bits bits 0000 1010
        // we add 1 :      0000 1011
        // the binary code : 2^3 + 2^1 + 2^0 = 11
        
        print(random1)
        //11
      
      
      
      
Coded tested with Swift 5.3 the 11 bebruary 2021

Introduction :


For do the Reverse Complement of two we need 3 things :

    • reverse the bits
      add one
      caster (cast for optimize the result)

  • When you do the complement with two at first we invert the bits (respectively we invert the 1 and the 0) then we add one to the binary number. We are at 5 = 101, we reverse the bits so we are at 010, and we add one to get 011 = 3 . The complement has two of 5 is therefore 3.

       010
    +    1
    -------
      011

    Let’s do the complement again with two, we start from 3. ( 011 in binary) We reverse the bits 100 and add one we get 101.

       100
    +    1
    -------
      101



    Let us take a concrete example: 5 in binary type Int, with the complement to two we will not get 5. Int is 64 bit on swift and 32 bit on java. We’re gonna have some of that:

    • 11111111 11111111 11111111 11111111 11111111 11111111 11111111 11111011
  • So we will need only the last three digits to cast in Int8 ( this is the value miniumum Int8 in swift in java is short). And we’re gonna get 5.

    Complement simple :

    Observations on 8 bits :

    Number unsigned number :

    00000000 - representing 0, the smallest number possible.

    11111111 -representing 255, the largest number possible,

    For a sum of 256 values (255 values + with value 0 )

    Whereas with sign magnitude we have :

    11111111 - representing -127, the smallest number possible.

    01111111 - representing 127, the largest number possible.

    For a sum of 255 value possibles

    > 4 = 0100

    -4 = 1011

    the maximum of the simple complement is:

    1000 0000 which represents:

    0111 1111 or: - 127 the first bit indicates whether the number is positive or negative in the simple complement.

    Why do we complement two?

    Not to encode on 64 bits (in Swift) but a number of smaller bits just needed.

    Why do we add one during the complement to two?

    If we do: -7+7 without adding a we get (0111 + 1000) which gives us -7 or the result should give us 0.

    Let’s try with the complement of two: -7 + 7 = ( 1000 + 1000 = 1 0000 or we code on 4 bit so we delete the 1 and we get 0000 which is geale at 0). Nn byte can represent values from 0 to 255.

    On 2 bytes 16 bit we can represent 2 15 value thus from 0 to ~ 32,000.

    However, we would like to be able to code negative numbers on these 8 bits ,

    we use the complement of two to have numbers from 0 to 127 and from -1 to 128.

    This allows you to guard a small number of bytes, if you only need a small value included on values ~ +-127.

    In addition to two on 16 bit ~ +- 16383 (2 15 -1) values

    (in addition to two in relation to the simple addition the number of available values is divided by two as one has a positive part equivalent to the negative part) If we need more value, we code on 8 bytes more and so on.



    source : https://ryanstutorials.net/binary-tutorial/binary-negative-numbers.php

    https://www.youtube.com/watch?v=phdt4r1I3gg