Jump to content

masking

- - - - -

This topic has been archived. This means that you cannot reply to this topic.
2 replies to this topic

#1
Chinmoy

Chinmoy

    Programming Expert

  • Members
  • PipPipPipPipPipPip
  • 392 posts
in this short tutorial we see the basic use of comparing and working with data at bit level. Here we check for odd and even numbers by comparing them at the bit level.



#include<stdio.h>

#include<conio.h>

void main()

{

	int a;

           clrscr();

	printf("Enter a number to check divisibility");

	scanf("%d",&a);

	if (a&1)

	{

		printf("\nOdd");

	}

	else

		printf("\nEven");

}



Here the number we enter is checked with the binary of one at the bit level.thus any number divisible with 2 will have 0 at the first place and 1 if it is odd. The & operation returns a TRUE to the if statement if the condition is true and the number is odd else not.

Attached Files

  • Attached File  1.JPG   4.48K   22 downloads


#2
WingedPanther

WingedPanther

    A spammer's worst nightmare

  • Moderators
  • 16,831 posts
Unfortunately, your code does not work. Try entering 15.
Programming is a branch of mathematics.
My CodeCall Blog | My Personal Blog

#3
Chinmoy

Chinmoy

    Programming Expert

  • Members
  • PipPipPipPipPipPip
  • 392 posts
oh i had something else in my mind!really sorry for that and thanks for pointing out..
ive edited the post.it'll work fine now.