Advertisement
Guest User

Untitled

a guest
Jun 17th, 2019
70
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.84 KB | None | 0 0
  1. int myFunction(int num)
  2. {
  3. if (num == 0)
  4.  
  5. // if number is 0, do not perform any operation.
  6. return 0;
  7. else
  8. // if number is power of 2, return 1 else return 0
  9.  
  10. num & (num - 1) == 0 ? return 1 : return 0
  11. }
  12.  
  13. cc -fPIC -shared -o dicmodule.so library.c
  14.  
  15. import ctypes
  16. NUM = 16
  17. # dicmodule loaded to the python file
  18. # using fun.myFunction(),
  19. # C function can be accessed
  20. # but type of argument is the problem.
  21.  
  22. fun = ctype.CDLL(dicmodule.so)
  23. # Now whenever argument
  24. # will be passed to the function
  25. # ctypes will check it.
  26.  
  27. fun.myFunction.argtypes(ctypes.c_int)
  28.  
  29. # now we can call this
  30. # function using instant (fun)
  31. # returnValue is the value
  32. # return by function written in C
  33. # code
  34. returnVale = fun.myFunction(NUM)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement