Simple Arguments and Return Values

One thing that C is good at is bit handling. In this section, you create a set of functions to handle a bit array. For simplicity, make it a fixed size (16×16).

You’ll need two C functions, one to set a bit to a given value and the other to return the value of the bit. Listing 14.2 contains the functions along with a little Perl code to test it.

Listing 14.2. bits1.pl
 use strict; use warnings; use Inline "C"; sub set_bit($$$); sub test_bit($$); set_bit(1,1,1); set_bit(1,2,1); if ((test_bit(1,1) != 1) || (test_bit(1,2) != 1) || (test_bit(1,3) != 0)) { die("Test #1 Failed"); } set_bit(1,1,0); if (test_bit(1,1) != 0) { die("Test #2 Failed"); } print "Successed!\n"; __END__ __C__ #define X_SIZE 16 #define Y_SIZE ...

Get Perl for C Programmers now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.