Exemplo n.º 1
0
def nim_sum ( i, j ):

#*****************************************************************************80
#
## NIM_SUM computes the Nim sum of two integers.
#
#  Discussion:
#
#    If K is the Nim sum of I and J, then each bit of K is the exclusive
#    OR of the corresponding bits of I and J.
#
#  Example:
#
#     I     J     K     I base 2    J base 2    K base 2
#   ----  ----  ----  ----------  ----------  ----------
#      0     0     0           0           0           0
#      1     0     1           1           0           1
#      1     1     0           1           1           0
#      2     7     5          10         111         101
#     11    28    23        1011       11100       10111
#
#  Licensing:
#
#    This code is distributed under the GNU LGPL license.
#
#  Modified:
#
#    26 May 2015
#
#  Author:
#
#    John Burkardt
#
#  Parameters:
#
#    Input, integer I, J, the integers to be Nim-summed.
#
#    Output, integer K, the Nim sum of I and J.
#
  from ubvec_to_ui4 import ubvec_to_ui4
  from ubvec_xor import ubvec_xor
  from ui4_to_ubvec import ui4_to_ubvec

  nbits = 32

  ivec = ui4_to_ubvec ( i, nbits )
  jvec = ui4_to_ubvec ( j, nbits )

  kvec = ubvec_xor ( nbits, ivec, jvec )

  k = ubvec_to_ui4 ( nbits, kvec )

  return k
Exemplo n.º 2
0
def nim_sum(i, j):

    #*****************************************************************************80
    #
    ## NIM_SUM computes the Nim sum of two integers.
    #
    #  Discussion:
    #
    #    If K is the Nim sum of I and J, then each bit of K is the exclusive
    #    OR of the corresponding bits of I and J.
    #
    #  Example:
    #
    #     I     J     K     I base 2    J base 2    K base 2
    #   ----  ----  ----  ----------  ----------  ----------
    #      0     0     0           0           0           0
    #      1     0     1           1           0           1
    #      1     1     0           1           1           0
    #      2     7     5          10         111         101
    #     11    28    23        1011       11100       10111
    #
    #  Licensing:
    #
    #    This code is distributed under the GNU LGPL license.
    #
    #  Modified:
    #
    #    26 May 2015
    #
    #  Author:
    #
    #    John Burkardt
    #
    #  Parameters:
    #
    #    Input, integer I, J, the integers to be Nim-summed.
    #
    #    Output, integer K, the Nim sum of I and J.
    #
    from ubvec_to_ui4 import ubvec_to_ui4
    from ubvec_xor import ubvec_xor
    from ui4_to_ubvec import ui4_to_ubvec

    nbits = 32

    ivec = ui4_to_ubvec(i, nbits)
    jvec = ui4_to_ubvec(j, nbits)

    kvec = ubvec_xor(nbits, ivec, jvec)

    k = ubvec_to_ui4(nbits, kvec)

    return k
Exemplo n.º 3
0
def ubvec_xor_test():

    #*****************************************************************************80
    #
    ## UBVEC_XOR_TEST tests UBVEC_XOR;
    #
    #  Licensing:
    #
    #    This code is distributed under the GNU LGPL license.
    #
    #  Modified:
    #
    #    26 May 2015
    #
    #  Author:
    #
    #    John Burkardt
    #
    from i4_uniform_ab import i4_uniform_ab
    from ui4_to_ubvec import ui4_to_ubvec
    from ubvec_to_ui4 import ubvec_to_ui4

    n = 10
    seed = 123456789
    test_num = 10

    print ''
    print 'UBVEC_XOR_TEST'
    print '  UBVEC_XOR exclusive-ors unsigned binary vectors representing'
    print '  unsigned integers;'
    print ''
    print '        I        J        K = I XOR J'
    print ''

    for test in range(0, test_num):
        i, seed = i4_uniform_ab(0, 100, seed)
        j, seed = i4_uniform_ab(0, 100, seed)
        bvec1 = ui4_to_ubvec(i, n)
        bvec2 = ui4_to_ubvec(j, n)
        bvec3 = ubvec_xor(n, bvec1, bvec2)
        k = ubvec_to_ui4(n, bvec3)
        print '  %8d  %8d  %8d' % (i, j, k)


#
#  Terminate.
#
    print ''
    print 'UBVEC_XOR_TEST'
    print '  Normal end of execution.'

    return
Exemplo n.º 4
0
def ubvec_xor_test ( ):

#*****************************************************************************80
#
## UBVEC_XOR_TEST tests UBVEC_XOR;
#
#  Licensing:
#
#    This code is distributed under the GNU LGPL license.
#
#  Modified:
#
#    26 May 2015
#
#  Author:
#
#    John Burkardt
#
  from i4_uniform_ab import i4_uniform_ab
  from ui4_to_ubvec import ui4_to_ubvec
  from ubvec_to_ui4 import ubvec_to_ui4

  n = 10
  seed = 123456789
  test_num = 10

  print ''
  print 'UBVEC_XOR_TEST'
  print '  UBVEC_XOR exclusive-ors unsigned binary vectors representing'
  print '  unsigned integers;'
  print ''
  print '        I        J        K = I XOR J'
  print ''

  for test in range ( 0, test_num ):
    i, seed = i4_uniform_ab ( 0, 100, seed )
    j, seed = i4_uniform_ab ( 0, 100, seed )
    bvec1 = ui4_to_ubvec ( i, n )
    bvec2 = ui4_to_ubvec ( j, n )
    bvec3 = ubvec_xor ( n, bvec1, bvec2 )
    k = ubvec_to_ui4 ( n, bvec3 )
    print '  %8d  %8d  %8d' % ( i, j, k )
#
#  Terminate.
#
  print ''
  print 'UBVEC_XOR_TEST'
  print '  Normal end of execution.'

  return
Exemplo n.º 5
0
def ui4_to_ubvec_test():

    #*****************************************************************************80
    #
    ## UI4_TO_UBVEC_TEST tests UI4_TO_UBVEC;
    #
    #  Licensing:
    #
    #    This code is distributed under the GNU LGPL license.
    #
    #  Modified:
    #
    #    26 May 2015
    #
    #  Author:
    #
    #    John Burkardt
    #
    from ubvec_to_ui4 import ubvec_to_ui4

    n = 10

    print ''
    print 'UI4_TO_UBVEC_TEST'
    print '  UI4_TO_UBVEC converts an unsigned integer to an'
    print '  unsigned binary vector;'
    print ''
    print '  UI4 --> UBVEC  -->  UI4'
    print ''

    for i in range(0, 11):
        bvec = ui4_to_ubvec(i, n)
        i2 = ubvec_to_ui4(n, bvec)
        print '  %2d  ' % (i),
        for i in range(0, n):
            print '%1d' % (bvec[i]),
        print '  %2d' % (i2)


#
#  Terminate.
#
    print ''
    print 'UI4_TO_UBVEC_TEST'
    print '  Normal end of execution.'

    return
Exemplo n.º 6
0
def ui4_to_ubvec_test ( ):

#*****************************************************************************80
#
## UI4_TO_UBVEC_TEST tests UI4_TO_UBVEC;
#
#  Licensing:
#
#    This code is distributed under the GNU LGPL license.
#
#  Modified:
#
#    26 May 2015
#
#  Author:
#
#    John Burkardt
#
  from ubvec_to_ui4 import ubvec_to_ui4

  n = 10

  print ''
  print 'UI4_TO_UBVEC_TEST'
  print '  UI4_TO_UBVEC converts an unsigned integer to an'
  print '  unsigned binary vector;'
  print ''
  print '  UI4 --> UBVEC  -->  UI4'
  print ''

  for i in range ( 0, 11 ):
    bvec = ui4_to_ubvec ( i, n )
    i2 = ubvec_to_ui4 ( n, bvec )
    print '  %2d  ' % ( i ),
    for i in range ( 0, n ):
      print '%1d' % ( bvec[i] ),
    print '  %2d' % ( i2 )
#
#  Terminate.
#
  print ''
  print 'UI4_TO_UBVEC_TEST'
  print '  Normal end of execution.'

  return
Exemplo n.º 7
0
def subset_gray_rank(n, a):

    #*****************************************************************************80
    #
    ## SUBSET_GRAY_RANK ranks a subset of an N set, using the Gray code ordering.
    #
    #  Example:
    #
    #    N = 4
    #
    #       A       Rank
    #    -------   -----
    #
    #    0 0 0 0       1
    #    1 0 0 0       2
    #    1 1 0 0       3
    #    0 1 0 0       4
    #    0 1 1 0       5
    #    1 1 1 0       6
    #    1 0 1 0       7
    #    0 0 1 0       8
    #    0 0 1 1       9
    #    1 0 1 1      10
    #    1 1 1 1      11
    #    0 1 1 1      12
    #    0 1 0 1      13
    #    1 1 0 1      14
    #    1 0 0 1      15
    #    0 0 0 1      16
    #
    #  Licensing:
    #
    #    This code is distributed under the GNU LGPL license.
    #
    #  Modified:
    #
    #    31 May 2015
    #
    #  Author:
    #
    #    John Burkardt
    #
    #  Parameters:
    #
    #    Input, integer N, the order of the total set from which
    #    subsets will be drawn.
    #
    #    Input, integer A(N); A(I) is 1 if element I is in the set,
    #    and 0 otherwise.
    #
    #    Output, integer RANK, the rank of the subset in the Gray code ordering.
    #
    from gray_rank2 import gray_rank2
    from ubvec_to_ui4 import ubvec_to_ui4

    gray = ubvec_to_ui4(n, a)

    rank = gray_rank2(gray)

    rank = rank + 1

    return rank
def subset_gray_rank ( n, a ):

#*****************************************************************************80
#
## SUBSET_GRAY_RANK ranks a subset of an N set, using the Gray code ordering.
#
#  Example:
#
#    N = 4
#
#       A       Rank
#    -------   -----
#
#    0 0 0 0       1
#    1 0 0 0       2
#    1 1 0 0       3
#    0 1 0 0       4
#    0 1 1 0       5
#    1 1 1 0       6
#    1 0 1 0       7
#    0 0 1 0       8
#    0 0 1 1       9
#    1 0 1 1      10
#    1 1 1 1      11
#    0 1 1 1      12
#    0 1 0 1      13
#    1 1 0 1      14
#    1 0 0 1      15
#    0 0 0 1      16
#
#  Licensing:
#
#    This code is distributed under the GNU LGPL license. 
#
#  Modified:
#
#    31 May 2015
#
#  Author:
#
#    John Burkardt
#
#  Parameters:
#
#    Input, integer N, the order of the total set from which
#    subsets will be drawn.
#
#    Input, integer A(N); A(I) is 1 if element I is in the set,
#    and 0 otherwise.
#
#    Output, integer RANK, the rank of the subset in the Gray code ordering.
#
  from gray_rank2 import gray_rank2
  from ubvec_to_ui4 import ubvec_to_ui4

  gray = ubvec_to_ui4 ( n, a )

  rank = gray_rank2 ( gray )

  rank = rank + 1

  return rank