C Programming Code Examples
C > Bitwise Operators Code Examples
program to check Most Significant Bit (MSB) of a number is set or not
/* program to check Most Significant Bit (MSB) of a number is set or not
Write a C program to input any number from user and check whether Most Significant Bit (MSB) of given number is set (1) or not (0). How to check whether Most Significant Bit of any given number is set or not using bitwise operator in C programming. C program to get the status of the most significant bit of a number.
We use bitwise AND & operator to check status of any bit. Bitwise AND operation evaluate each bit of resultant value as 1, if corresponding bit of operands is 1.
Input a number from user. Store it in some variable say j.
Find number of bits required to represent an integer in memory. Use sizeof() operator to find size of integer in bytes. Then multiply it by 8 to get number of bits required by integer. Store total bits in some variable say bits = sizeof(int) * 8;.
To get MSB of the number, move first bit of 1 to highest order. Left shift 1 bits - 1 times and store result in some variable say msb = 1 << (bits - 1).
If bitwise AND operation j & msb evaluate to 1 then MSB of j is set otherwise not.
C program to check Most Significant Bit (MSB) of a number using bitwise operator */
#include <stdio.h>
#define BITS sizeof(int) * 8 // Total bits required to represent integer
int main()
{
int j, msb;
/* Input number from user */
printf("Enter any number: ");
scanf("%d", &j);
/* Move first bit of 1 to highest order */
msb = 1 << (BITS - 1);
/* Perform bitwise AND with msb and j */
if(j & msb)
printf("MSB of %d is set (1).", j);
else
printf("MSB of %d is unset (0).", j);
return 0;
}
The sizeof() operator is commonly used in C. It determines the size of the expression or the data type specified in the number of char-sized storage units. The sizeof() operator contains a single operand which can be either an expression or a data typecast where the cast is data type enclosed within parenthesis. The data type cannot only be primitive data types such as integer or floating data types, but it can also be pointer data types and compound data types such as unions and structs.
Writes the C string pointed by format to the standard output (stdout). If format includes format specifiers (subsequences beginning with %), the additional arguments following format are formatted and inserted in the resulting string replacing their respective specifiers. printf format string refers to a control parameter used by a class of functions in the input/output libraries of C programming language. The string is written in a simple template language: characters are usually copied literally into the function's output, but format specifiers, which start with a % character, indicate the location and method to translate a piece of data (such as a number) to characters. "printf" is the name of one of the main C output functions, and stands for "print formatted". printf format strings are complementary to scanf format strings, which provide formatted input (parsing). In both cases these provide simple functionality and fixed format compared to more sophisticated and flexible template engines or parsers,
The bitwise operators are the operators used to perform the operations on the data at the bit-level. When we perform the bitwise operations, then it is also known as bit-level programming. It consists of two digits, either 0 or 1. It is mainly used in numerical computations to make the calculations faster. We have different types of bitwise operators in the C programming language. The following is the list of the bitwise operators: Bitwise AND operator is denoted by the single ampersand sign (&). Two integer operands are written on both sides of the (&) operator. If the corresponding bits of both the operands are 1, then the output of the bitwise AND operation is 1; otherwise, the output would be 0.
#include is a way of including a standard or user-defined file in the program and is mostly written at the beginning of any C/C++ program. This directive is read by the preprocessor and orders it to insert the content of a user-defined or system header file into the following program. These files are mainly imported from an outside source into the current program. The process of importing such files that might be system-defined or user-defined is known as File Inclusion. This type of preprocessor directive tells the compiler to include a file in the source code program. Here are the two types of file that can be included using #include:
Read formatted data from stdin. Reads data from stdin and stores them according to the parameter format into the locations pointed by the additional arguments. The additional arguments should point to already allocated objects of the type specified by their corresponding format specifier within the format string. In C programming, scanf() is one of the commonly used function to take input from the user. The scanf() function reads formatted input from the standard input such as keyboards. The scanf() function enables the programmer to accept formatted inputs to the application or production code. Moreover, by using this function, the users can provide dynamic input values to the application.
The if-else statement is used to perform two operations for a single condition. The if-else statement is an extension to the if statement using which, we can perform two different operations, i.e., one is for the correctness of that condition, and the other is for the incorrectness of the condition. Here, we must notice that if and else block cannot be executed simiulteneously. Using if-else statement is always preferable since it always invokes an otherwise case with every if condition.
In the C Programming Language, the #define directive allows the definition of macros within your source code. These macro definitions allow constant values to be declared for use throughout your code. Macro definitions are not variables and cannot be changed by your program code like variables. You generally use this syntax when creating constants that represent numbers, strings or expressions.
In C, the "main" function is treated the same as every function, it has a return type (and in some cases accepts inputs via parameters). The only difference is that the main function is "called" by the operating system when the user runs the program. Thus the main function is always the first code executed when a program starts. main() function is a user defined, body of the function is defined by the programmer or we can say main() is programmer/user implemented function, whose prototype is predefined in the compiler. Hence we can say that main() in c programming is user defined as well as predefined because it's prototype is predefined. main() is a system (compiler) declared function whose defined by the user, which is invoked automatically by the operating system when program is being executed.
Firstly Declare two file pointers for two files. Open 2 files in read mode. Now Inside while loop read both files character by character. Check both characters whether they're equal
Buffer for ungetch, next free position in buf, word frequency count, getword: get next word or character from input, addtree: add a node with w, at or below p, a new word has
Abbreviated weekday name, Full weekday name, Abbreviated month name, Full month name, Standard date and time string, Last two digits of year, Day of month as a decimal
C program example to learn reference enum value by int. Press a key to select animal: 1: cat, 2: dog, 3: lion, 4: tiger. Generate a new random number. Read and discard character.
Your time has started. Start typing. Hit Esc when done. Erase previous character instead of cursoring. Eliminate a special character that is printed for Esc. A Backspace followed
We have to sort only when left<right because when left=right it is anyhow sorted. Sort the left part. Sort the right part. Merge the two sorted parts. Merge functions merges the...