Пример #1
0
void main()
{
	int n,set,clr,tog,res,check;
	printf("Enter the number:\n");
	scanf("%d",&n);
	printf("Position to set:\n");
	scanf("%d",&set);
	printf("Position to clear:\n");
	scanf("%d",&clr);
	printf("Position to toggle:\n");
	scanf("%d",&tog);
	printf("Position to check:\n");
	scanf("%d",&check);
	res=setbit(n,set);
	printf("Result after set is :%d \n",res);
	res=clearbit(n,clr);
	printf("Result after clear is :%d \n",res);
	res=togglebit(n,tog);
	printf("Result after toggle is :%d \n",res);
	res=checkbit(n,check);
	if(res)
		printf("The %d th bit in %d is  set\n",check,n);
	else
		printf("The %d th bit in %d is  not set\n",check,n);
}
Пример #2
0
int
main ()
{
  int c;
  unsigned int x = 15;          // 1111

  printf ("x = %d (1111)\n\n", 15);

  printf ("x ^ 0 = %d   (x)\n", x ^ 0);
  printf ("x ^ 1 = %d   ~(x)\n", x ^ 15);
  printf ("x ^ x = %d    (0)\n\n", x ^ x);

  printf ("x & 0 = %d    (0)\n", x & 0);
  printf ("x & 1 = %d   (x)\n", x & 15);
  printf ("x & x = %d   (x)\n\n", x & x);

  printf ("x | 0 = %d   (x)\n", x | 0);
  printf ("x | 1 = %d   (1)\n", x | 15);
  printf ("x | x = %d   (x)\n\n", x | x);


  printf ("x = %d (1010)\n", x = 10);
  for (c = 3; c >= 0; --c)
    printf ("get %d bit: %d\n", c, getbit (x, c));

  printf ("\n");
  printf ("set bit %d to 1: %d\n", 2, x = setbit (x, 2));
  for (c = 3; c >= 0; --c)
    printf ("get %d bit: %d\n", c, getbit (x, c));

  printf ("\n");
  printf ("clear bit %d to 0: %d\n", 3, x = clearbit (x, 3));
  for (c = 3; c >= 0; --c)
    printf ("get %d bit: %d\n", c, getbit (x, c));

  printf ("\n");
  printf ("x = %d (1111)\n", x = 15);
  printf ("clear most significant bits through bit %d: %d\n", 2,
      x = clearbits_msb (x, 2));
  for (c = 3; c >= 0; --c)
    printf ("get %d bit: %d\n", c, getbit (x, c));

  printf ("\n");
  printf ("x = %d (1111)\n", x = 15);
  printf ("clear least significant bits through bit %d: %d\n", 1,
      x = clearbits_lsb (x, 1));
  for (c = 3; c >= 0; --c)
    printf ("get %d bit: %d\n", c, getbit (x, c));

  printf ("\n");
  printf ("x = %d (1111)\n", x = 15);
  printf ("update bit %d to 0: %d\n", 2, x = updatebit (x, 2, 0));
  printf ("update bit %d to 1: %d\n", 3, x = updatebit (x, 3, 1));
  for (c = 3; c >= 0; --c)
    printf ("get %d bit: %d\n", c, getbit (x, c));

  printf ("\n");
  printf ("x = %d (1111)\n", x = 15);
  printf ("update bit %d to 0 (C support): %d\n", 2, x = updatebit_c (x, 2, 0));
  printf ("update bit %d to 1 (C support): %d\n", 3, x = updatebit_c (x, 3, 1));
  for (c = 3; c >= 0; --c)
    printf ("get %d bit: %d\n", c, getbit (x, c));

  printf ("\n");
  printf ("toggle bit %d: %d\n", 1, x = togglebit (x, 1));
  printf ("toggle bit %d: %d\n", 3, x = togglebit (x, 3));
  for (c = 3; c >= 0; --c)
    printf ("get %d bit: %d\n", c, getbit (x, c));
}