#include <stdio.h>
int main()
{
typedef int bool;
bool f; /* conditional flag */
unsigned int m; /* the bit mask */
unsigned int w; /* the word to modify: if (f) w |= m; else w &= ~m; */
printf("Enter the word to modify ");
scanf("%u",&w);
printf("Enter the bit mask ");
scanf("%u",&m);
/* w = w ^ ((-f ^ w) & m ); */
/* OR, for superscalar CPUs: */
w = (w & ~m) | (-f & m);
printf("Modified value is %u \n",w);
return 0;
}
Hacking the program for counting bits set
$gcc counting-bits-set-naive-way-related.c
$./a.out
Enter v 8474578
Total bits set in v is 11
$./a.out
Enter v 1
Total bits set in v is 1
$./a.out
Enter v 2
Total bits set in v is 1
$./a.out 3
Enter v a
Total bits set in v is 0
$cat counting-bits-set-naive-way-related.c
#include <stdio.h>
int main()
{
unsigned int v; /* count the number of bits set in v */
unsigned int c; /* c accumulates the total bits set in v */
printf("Enter v ");
scanf("%u",&v);
for(c = 0; v ; v >>= 1)
{
c = c + (v & 1);
}
printf("Total bits set in v is %u \n",c);
return 0;
}
$
Hacking bitwise left shift operation with PHP
<?php
$val = 1;
for($count = 0; $count <=10 ; $count++) {
echo "count is ..." . $count;
echo "\n";
echo "val is ..." . $val;
echo "\n";
$val = $val << 1;
}
?>
How to find out if nth bit of a number is set to 1?
#include <stdio.h>
int main()
{
int val1 = 4;
int val2 = 18;
int mask = 1;
/* mask <<= 4; */
mask = mask << 4;
printf("mask is %d\n",mask);
if ( val1 & mask ) {
printf("Bit 5 in val1 is set\n");
}
if (val2 & mask ) {
printf("Bit 5 in val2 is set\n");
}
}
How to write a program to find if a number is a power of 2 ?
/* determining if an integer is a power of 2 */
#include <stdio.h>
int main()
{
unsigned int v; /* we want to see if v is a power of 2 */
typedef int bool;
bool f;
printf("enter number");
scanf("%d",&v);
f = ( v & ( v - 1) ) == 0;
printf("the result is %d \n",f);
}
How to write a simple C program showing addition ?
$cat add-p1.c
#include <stdio.h>
#include <stdlib.h>
int add (int a,int b) {
return a + b;
}
int main() {
int a,b;
a =3;
b =4;
int ret = add(a,b);
printf("Result: %u\n",ret);
exit(0);
}
$gcc add-p1.c
$./a.out
Result: 7
$
New American Bible – Proverbs 1:5
The wise by hearing them will advance in learning,
the intelligent will gain sound guidance,
the intelligent will gain sound guidance,
How to check exit status of a command ?
$stat stat: missing operand Try 'stat --help' for more information. $echo $? 1 $stat popen1-p1.c File: ‘popen1-p1.c’ Size: 473 Blocks: 8 IO Block: 4096 regular file Device: 801h/2049d Inode: 5245647 Links: 1 Access: (0644/-rw-r--r--) Uid: ( 1000/ jeffrin) Gid: ( 1000/ jeffrin) Access: 2014-06-16 23:31:58.746088758 +0530 Modify: 2014-06-16 23:31:27.649735653 +0530 Change: 2014-06-16 23:31:27.649735653 +0530 Birth: - $echo $? 0 $uname Linux $echo $? 0 $uname -top uname: invalid option -- 't' Try 'uname --help' for more information. $echo $? 1 $
How to compile a program to use electric fence library for debugging ?
$cat efence-p1.c
#include <stdio.h>
#include <stdlib.h>
int main()
{
char *ptr = (char *) malloc(1024);
ptr[0] = 0;
ptr[1024] = 0;
exit(0);
}
$gcc -g efence-p1.c -lefence
$./a.out
Electric Fence 2.2 Copyright (C) 1987-1999 Bruce Perens <bruce@perens.com>
Segmentation fault
$
Hacking with a program which has initialization of a MYSQL object
$ls connect1-p1.c $cp connect1-p1.c connect1-p2.c $emacs connect1-p2.c $gcc -lmysqlclient -g -L/usr/lib/mysql connect1-p2.c $./a.out mysql_init failed $echo $? 1 $./a.out 2> error.txt $cat error.txt mysql_init failed $emacs connect1-p2.c $gcc -lmysqlclient -g -L/usr/lib/mysql connect1-p2.c $./a.out mysql_init failed $./a.out 2> error.txt mysql_init failed $cat error.txt $