Hi All,
In this post, i would like to show frequently asked C-FAQs that are being encountered in interviews.
BIT-WISE Operators C-Faqs:
1. How to set particular bit in a number?
2. How to clear particular bit in a number?
3. How to toggle or flip particular bit in a number?
To toggle any bit in a number, Use (^ ) exclusive OR operator.
Number = Number ^ (1<<x)
4.How to check particular bit is set or not in a number?
To check any bit in a number, Use (& ) And Operator .
'x' indicates the position of the bit
5. How to represent the above in MACRO's for Real time code?
#define SET_BIT(Number, bit_position) Number |= (1<< bit_position)
#define CLEAR_BIT(Number, bit_position) Number &= ~(1<< bit_position)
#define TOGGLE_BIT(Number, bit_position) Number ^= (1<< bit_position)
#define CHECK_BIT_IS_SET_OR_NOT(Number, bit_position) Number & (1<< bit_position)
6. How to check the number is Even or Odd?
6. How to count number of 1's or bits set in a number ?
#include<stdio.h>
int main()
{
int n, count=0;
printf(" Enter the Number :\n");
scanf(" %d", &n);
while(n)
{
n = n&(n-1);
count ++;
} printf(" No of Bits set count in the Number is %d\n", count);
return 0;
}
7. How to count number of 0's or bits unset in a number ?
#include<stdio.h>
#define CHAR_BYTE 8
int main()
{
unsigned int num, count =0, index;
printf(" Enter the Integer \n");
scanf("%d", &num);
for(index = 0; index < (CHAR_BYTE * sizeof(int)); index++)
{
/* check the Bit is Set to Zero */
if((num & (1 << index)) == 0)
{
count ++;
}
}
printf(" The Number of Zero's present in Integer: %d\n", count);
return 0;
}
8. How to find out a number is power of 2 or not?
#include<stdio.h>
int main()
{
unsigned int n;
printf("Enter the Number \n");
scanf("%d", &n);
if((n != 0) && (n & (n-1)) )
printf(" It is Not power of 2\n");
else
printf(" It is power of 2\n");
return 0;
}
9. How to Swap two numbers using Bit Wise Operators? ( Remember as -> (ab, ba, ab)
#include<stdio.h>
int main()
{
int a, b;
printf(" Enter the Value of a and b\n");
scanf("%d %d", &a, &b);
a = a ^ b;
b = b ^ a;
a = a ^ b;
/*
or
a = a + b;
b = a - b;
a = a - b; */
printf("Value of a : %d and b : %d\n", a, b);
return 0;
}
10. How to reverse a string with out using Temporary variable?
#include<stdio.h>
#include<string.h>
#define LEN 100 /* Can be changed */
int main()
{
char str[LEN];
unsigned int end, start=0;
printf(" Enter the String to be reversed \n");
scanf("%s", str);
/* Find out the string Length */
end = strlen(str)-1;
/* Iterate through start less than end */
while(start < end)
{
str[start] ^= str[end];
str[end] ^= str[start];
str[start] ^= str[end];
start++;
end --;
}
printf("The Reverse string is : %s\n", str);
return 0;
}
10. How to reverse Bits of a Number?
#include<stdio.h>
#define SIZE_OF_CHAR 8
int main()
{
unsigned int i, num, reverse_num= 0;
unsigned int NO_OF_BITS;
printf("Enter the number to be reversed \n");
scanf("%d", &num);
NO_OF_BITS = SIZE_OF_CHAR * (sizeof(num));
for(i=0; i<NO_OF_BITS; i++)
{
if(num & (1<<i)) -> This similar Logic is used to count 1's & 0's in a number.
{
reverse_num = reverse_num | (1<< ((NO_OF_BITS -1)-i));
}
}
printf(" The reverse Num is %d", reverse_num);
}
Please refer the BITMAP tutorial in the BLOG, It is really useful for real time applications.
In this post, i would like to show frequently asked C-FAQs that are being encountered in interviews.
BIT-WISE Operators C-Faqs:
1. How to set particular bit in a number?
To set any bit in a number, Use (OR ) operator.
Number = Number | (1<<x)
'x' indicates the position of the bit
2. How to clear particular bit in a number?
To clear any bit in a number, Use (AND ) and (NEG (~)) operator.
Number = Number & ~(1<<x)
3. How to toggle or flip particular bit in a number?
To toggle any bit in a number, Use (^ ) exclusive OR operator.
4.How to check particular bit is set or not in a number?
To check any bit in a number, Use (& ) And Operator .
Number & (1<<x)
5. How to represent the above in MACRO's for Real time code?
#define SET_BIT(Number, bit_position) Number |= (1<< bit_position)
#define CLEAR_BIT(Number, bit_position) Number &= ~(1<< bit_position)
#define TOGGLE_BIT(Number, bit_position) Number ^= (1<< bit_position)
#define CHECK_BIT_IS_SET_OR_NOT(Number, bit_position) Number & (1<< bit_position)
6. How to check the number is Even or Odd?
#include<stdio.h>
int main()
{
int number;
printf("Enter the Number \n");
scanf("%d", &number);
if(number & 1)
printf(" It is ODD number \n");
else
printf(" It is Even number \n");
}
int main()
{
int number;
printf("Enter the Number \n");
scanf("%d", &number);
if(number & 1)
printf(" It is ODD number \n");
else
printf(" It is Even number \n");
}
6. How to count number of 1's or bits set in a number ?
#include<stdio.h>
int main()
{
int n, count=0;
printf(" Enter the Number :\n");
scanf(" %d", &n);
while(n)
{
n = n&(n-1);
count ++;
} printf(" No of Bits set count in the Number is %d\n", count);
return 0;
}
7. How to count number of 0's or bits unset in a number ?
#include<stdio.h>
#define CHAR_BYTE 8
int main()
{
unsigned int num, count =0, index;
printf(" Enter the Integer \n");
scanf("%d", &num);
for(index = 0; index < (CHAR_BYTE * sizeof(int)); index++)
{
/* check the Bit is Set to Zero */
if((num & (1 << index)) == 0)
{
count ++;
}
}
printf(" The Number of Zero's present in Integer: %d\n", count);
return 0;
}
8. How to find out a number is power of 2 or not?
#include<stdio.h>
int main()
{
unsigned int n;
printf("Enter the Number \n");
scanf("%d", &n);
if((n != 0) && (n & (n-1)) )
printf(" It is Not power of 2\n");
else
printf(" It is power of 2\n");
return 0;
}
9. How to Swap two numbers using Bit Wise Operators? ( Remember as -> (ab, ba, ab)
#include<stdio.h>
int main()
{
int a, b;
printf(" Enter the Value of a and b\n");
scanf("%d %d", &a, &b);
a = a ^ b;
b = b ^ a;
a = a ^ b;
/*
or
a = a + b;
b = a - b;
a = a - b; */
printf("Value of a : %d and b : %d\n", a, b);
return 0;
}
10. How to reverse a string with out using Temporary variable?
#include<stdio.h>
#include<string.h>
#define LEN 100 /* Can be changed */
int main()
{
char str[LEN];
unsigned int end, start=0;
printf(" Enter the String to be reversed \n");
scanf("%s", str);
/* Find out the string Length */
end = strlen(str)-1;
/* Iterate through start less than end */
while(start < end)
{
str[start] ^= str[end];
str[end] ^= str[start];
str[start] ^= str[end];
start++;
end --;
}
printf("The Reverse string is : %s\n", str);
return 0;
}
10. How to reverse Bits of a Number?
#include<stdio.h>
#define SIZE_OF_CHAR 8
int main()
{
unsigned int i, num, reverse_num= 0;
unsigned int NO_OF_BITS;
printf("Enter the number to be reversed \n");
scanf("%d", &num);
NO_OF_BITS = SIZE_OF_CHAR * (sizeof(num));
for(i=0; i<NO_OF_BITS; i++)
{
if(num & (1<<i)) -> This similar Logic is used to count 1's & 0's in a number.
{
reverse_num = reverse_num | (1<< ((NO_OF_BITS -1)-i));
}
}
printf(" The reverse Num is %d", reverse_num);
}
Please refer the BITMAP tutorial in the BLOG, It is really useful for real time applications.
nice and short explanation
ReplyDeleteHello Sekhar,
DeleteBest thing I have read in a while on this Linux Networking And Useful Tips for Real-Time Applications. There should be a standing ovation button. This is a great piece.
Some suggestions/advice/insight as to which Linux version to install.
The most important difference between Ext2 and Ext3 is that Ext3 supports journaling. After an unexpected power failure or system crash (also called an unclean system shutdown), each mounted ext2 file system on the machine must be checked for consistency by the e2fsck program.
I am a quadriplegic, so… Maybe a version that doesn't have too much problem with running a virtual box so I can install Dragon NaturallySpeaking within this virtual box.
Anyways great write up, your efforts are much appreciated.
Obrigado,
Kevin
I have one doubt
ReplyDeleteFor counting number of 0's or bits unset in a number we use
if((num & (1 << index)) == 0)
{
count ++;
}
Can't we use the similar below logic to check number of 1's in a number
if((num & (1 << index)) == 1) =====> instead of if((num & (1 << index)) == 0)
{
count ++;
}
n = n&(n-1); will reduce the number of iterations
DeleteHey Brother,
ReplyDeleteAmaze! I have been looking bing for hours because of this and i also in the end think it is in this article! Maybe I recommend you something helps me all the time?
First and foremost I'd like to say I'm very excited to be a part of the Python world which is truly beautiful and limitless (I'm about to find out that).
My goal is to make a simple script that would copy all the settings in Windows related to the mouse (windows sensitivity, DPI, pointer precision etc.) from my computer in home and with that information paste all that information to my Windows at work.
I'm trying to play games competitively and I'm noticing that once I get used to the sensitivity at work for 8 hours it takes an uncomfortable while and deaths in games to get into the sensitivity here.
I wasn't able to find nothing related to such information gathering/setting afterwards.
I'm not necessarily looking for a complete script(even though I could use it as a guidance), but rather a specific module or tutorial that would bump me into creating such script myself.
THANK YOU!! This saved my butt today, I’m immensely grateful.
Regards,
Irene Hynes
python training in bangalore | python online training
ReplyDeleteAWS training in Bangalore | aws online training
artificial intelligence training in bangalore | artificial intelligence training
machine learning training in bangalore | machine learning online training
data science training in bangalore | data science online training
I am reading your post from the beginning, it was so interesting to read & I feel thanks to you for posting such a good blog, keep updates regularly.
ReplyDeleteJava training in Chennai
Java Online training in Chennai
Java Course in Chennai
Best JAVA Training Institutes in Chennai
Java training in Bangalore
Java training in Hyderabad
Java Training in Coimbatore
Java Training
Java Online Training
It is perfect time to make some plans for the future and it is time to be happy. I’ve read this post and if I could I desire to suggest you few interesting things or tips.highly informative and professionally written and I am glad to be a visitor of this perfect blog, thank you
ReplyDeleteselenium training in chennai
selenium training in chennai
selenium online training in chennai
software testing training in chennai
selenium training in bangalore
selenium training in hyderabad
selenium training in coimbatore
selenium online training
selenium training
I just got to this amazing site not long ago. I was actually captured with the piece of resources you have got here. Big thumbs up for making such wonderful blog page. Thanks for sharing this information.
ReplyDeleteangular js training in chennai
angular training in chennai
angular js online training in chennai
angular js training in bangalore
angular js training in hyderabad
angular js training in coimbatore
angular js training
angular js online training
Nice post. Thanks for sharing! I want people to know just how good this information is in your article. It’s interesting content and Great work.Very useful and informative content has been shared out here, Thanks for sharing it
ReplyDeleteAzure Training in Chennai
Azure Training in Bangalore
Azure Training in Hyderabad
Azure Training in Pune
Azure Training | microsoft azure certification | Azure Online Training Course
Azure Online Training
wow really nice. It will be helpful for the people those who are ready to crack the interview and please also for remind what they have learned throughout concept.This concept is a good way to enhance the knowledge.thanks for sharing.
ReplyDeleteDevOps Training in Chennai
DevOps Online Training in Chennai
DevOps Training in Bangalore
DevOps Training in Hyderabad
DevOps Training in Coimbatore
DevOps Training
DevOps Online Training
I just got to this amazing site not long ago. I was actually captured with the piece of resources you have got here. Big thumbs up for making such wonderful blog page!
ReplyDeleteAWS Course in Bangalore
AWS Course in Hyderabad
AWS Course in Coimbatore
AWS Course
AWS Certification Course
AWS Certification Training
AWS Online Training
AWS Training
I just got to this amazing site not long ago. I was actually captured with the piece of resources you have got here. Big thumbs up for making such wonderful blog page!
ReplyDeleteacte chennai
acte complaints
acte reviews
acte trainer complaints
acte trainer reviews
acte velachery reviews complaints
acte tambaram reviews complaints
acte anna nagar reviews complaints
acte porur reviews complaints
acte omr reviews complaints
Great post! I am actually getting ready to across this information, It’s very helpful for this blog. Also great with all of the valuable information you have Keep up the good work you are doing well.
ReplyDeleteIELTS Coaching in chennai
German Classes in Chennai
GRE Coaching Classes in Chennai
TOEFL Coaching in Chennai
spoken english classes in chennai | Communication training
I feel really happy to have seen your webpage.I am feeling grateful to read this.you gave a nice information for us.please updating more stuff content...keep up!!
ReplyDeleteData Science Training In Chennai
Data Science Online Training In Chennai
Data Science Training In Bangalore
Data Science Training In Hyderabad
Data Science Training In Coimbatore
Data Science Training
Data Science Online Training