Change:
print "result for AND operation on $bit is $result \n";
to:
print "result for AND operation on $p is $result \n";
Then, you can localize $bit
to the foreach
loop:
foreach my $bit (@c) {
The foreach loop iterates over a normal list value and sets the variable VAR to be each element of the list in turn. If the variable is preceded with the keyword my, then it is lexically scoped, and is therefore visible only within the loop. Otherwise, the variable is implicitly local to the loop and regains its former value upon exiting the loop. If the variable was previously declared with my, it uses that variable instead of the global one, but it's still localized to the loop.
$bit
was uninitialized before the loop, so it is still unchanged after the loop.