fork(1) download
  1. # http://c...content-available-to-author-only...e.com/q/74535/34718
  2. # http://c...content-available-to-author-only...e.com/a/74550/34718
  3.  
  4. def f(a,b,c=1,d=0):
  5. z=2**c-1<<b;return[a^z,a|z,a&~z][cmp(d,0)]
  6. g=lambda a,b,c=1:a>>b&2**c-1
  7.  
  8.  
  9. print f(0, 2) #=> 4 (0 => 100)
  10. print f(4, 2) #=> 0 (100 => 0)
  11. print f(0, 2, 4) #=> 60 (0 => 111100)
  12. print f(170, 2, 4, 0) #=> 150 (10101010 => 10010110)
  13. print f(170, 2, 4, 1) #=> 190 (10101010 => 10111110)
  14. print f(170, 2, 4, -1) #=> 130 (10101010 => 10000010)
  15.  
  16. print
  17. print
  18.  
  19. print g(0, 2) #=> 0 (0 => 0)
  20. print g(4, 2) #=> 1 (100 => 1)
  21. print g(60, 2, 4) #=> 15 (111100 => 1111)
  22. print g(170, 2, 4) #=> 10 (10101010 => 1010)
  23.  
Success #stdin #stdout 0.01s 9016KB
stdin
Standard input is empty
stdout
4
0
60
150
190
130


0
1
15
10