A comprehensive, manually-coded approach to solving high-level statistical computing problems.
Bitwise operators are special operators in the C programming language that operate directly on the binary representation of integers. Instead of working with decimal values, these operators manipulate individual bits (0 or 1) of a number.
Bitwise operations are widely used in low-level programming, system software, device drivers, cryptography, and applications where efficient memory and performance optimization are required.
The commonly used bitwise operators in C are:
| Operator | Name | Description |
|---|---|---|
& | Bitwise AND | Returns 1 if both bits are 1 |
| | Bitwise OR | Returns 1 if at least one bit is 1 |
^ | Bitwise XOR | Returns 1 if bits are different |
~ | Bitwise NOT | Inverts all bits |
&)The bitwise AND operator compares each bit of two numbers. The result bit is 1 only when both corresponding bits are 1.
| Bit of 5 | Bit of 3 | Result |
|---|---|---|
| 0 | 0 | 0 |
| 1 | 0 | 0 |
| 0 | 1 | 0 |
| 1 | 1 | 1 |
Thus, the result of the AND operation is 1.
|)The bitwise OR operator compares each bit of two numbers. The result bit is 1 if at least one of the corresponding bits is 1.
Thus, the OR operation produces the value 7.
^)The bitwise XOR (exclusive OR) operator returns 1 when the corresponding bits of two numbers are different and returns 0 when they are the same.
Hence the result is 6.
~)The bitwise NOT operator inverts all bits of a number. Every 0 becomes 1 and every 1 becomes 0.
In most systems using two's complement representation, this corresponds to the value: $\sim 5 = -6$
| Operator | Name | Description |
|---|---|---|
<< | Left Shift | Shifts bits to the left |
>> | Right Shift | Shifts bits to the right |
<<)Each bit shifts one position to the left and a 0 is inserted at the rightmost position. Thus, left shift is equivalent to multiplying the number by $2^n$.
>>)Each bit moves one position to the right. Thus, right shift is equivalent to dividing the number by $2^n$.
Write a C program to determine whether a given year is a leap year or not.
A leap year is a year that contains 366 days instead of 365 days. The extra day is added to the month of February.
A year is considered a leap year if it satisfies the following conditions:
Examples: 2000 is a leap year. 1900 is not a leap year. 2024 is a leap year.
#include <stdio.h>
int main()
{
int year;
printf("Enter a year: ");
scanf("%d", &year);
if((year % 400 == 0) || (year % 4 == 0 && year % 100 != 0))
printf("The year is a Leap Year");
else
printf("The year is Not a Leap Year");
return 0;
}
Write a C program to calculate the sum of the following series:
The given series follows a specific arithmetic pattern where:
The last term of the series is $\frac{99}{100}$. The program calculates the cumulative sum using a single loop, ensuring proper typecasting to handle floating-point division.
sum variable to zero (as a float).for loop initializing i = 1 and j = 2.i by j.sum.i and j by 4.i exceeds 99.sum.#include <stdio.h>
int main()
{
int i, j;
float sum = 0;
/* Loop steps numerator and denominator by 4 simultaneously */
for(i = 1, j = 2; i <= 99; i = i + 4, j = j + 4)
{
/* Explicitly cast 'i' to float to prevent integer division */
sum = sum + (float)i / j;
}
printf("Sum of the series = %f", sum);
return 0;
}
i : Represents the numerator values (1, 5, 9, ..., 99).j : Represents the denominator values (2, 6, 10, ..., 100).sum : Stores the cumulative sum of the fractional series.for loop and utilizing explicit typecasting, it calculates the final sum efficiently.
File handling in C allows programs to store data permanently in external files. This enables data to be saved, processed, and retrieved even after the program's execution is completed.
Instead of storing data temporarily in RAM (which is volatile), file operations allow direct interaction with the hard drive. The C standard library provides several functions to perform these critical operations.
fopen() - Open a FileThis function is used to open an existing file or create a new one. It returns a file pointer that is used to access the file in subsequent operations.
r : Read modew : Write mode (overwrites existing data)a : Append mode (adds data to the end)FILE *fp;
fp = fopen("data.txt", "r");
fclose() - Close a FileOnce all operations are completed, it is mandatory to close the file to free up memory resources and ensure all buffers are flushed to the disk.
fclose(fp);
fprintf() - Write Formatted DataSimilar to printf(), but instead of writing to the console screen, it writes formatted text directly into the specified file.
fprintf(fp, "Student Name: %s, Marks: %d", name, marks);
fscanf() - Read Formatted DataSimilar to scanf(), it reads formatted data from the file instead of the standard input (keyboard).
int num;
fscanf(fp, "%d", &num);
fgetc() and fputc() - Character I/OThese functions are used to read (fgetc) and write (fputc) a single character from or to a file. They are often used in loops to process files character by character.
char ch;
/* Read a character */
ch = fgetc(fp);
/* Write a character */
fputc(ch, fp);
fopen, fclose, and various read/write commands, programs can handle large-scale data processing applications and robust database systems efficiently.
Write a set of R codes to perform the following operations:
In matrix theory, two highly important values can be computed from a matrix to determine optimal strategies or saddle points:
If these two values are equal, that specific value is called the saddle point of the matrix.
To demonstrate computational proficiency, this program constructs the matrix from a given vector and computes the MinMax and MaxMin values manually using nested loops, entirely avoiding R's built-in min() and max() functions.
# Step 1: Input vector
v <- c(4, 7, 2, 9, 5, 6)
# Step 2: Matrix dimensions
r1 <- 2
c1 <- 3
# Step 3: Create matrix manually
m <- matrix(0, r1, c1)
k <- 1
for(i in 1:r1) {
for(j in 1:c1) {
m[i,j] <- v[k]
k <- k + 1
}
}
cat("Matrix is:\n")
print(m)
# Step 4: Compute row minimums
row_min <- numeric(r1)
for(i in 1:r1) {
min_val <- m[i,1]
for(j in 2:c1) {
if(m[i,j] < min_val) {
min_val <- m[i,j]
}
}
row_min[i] <- min_val
}
# Step 5: Compute column maximums
col_max <- numeric(c1)
for(j in 1:c1) {
max_val <- m[1,j]
for(i in 2:r1) {
if(m[i,j] > max_val) {
max_val <- m[i,j]
}
}
col_max[j] <- max_val
}
# Step 6: Find MinMax (Minimum of column maximums)
minmax <- col_max[1]
for(j in 2:c1) {
if(col_max[j] < minmax) {
minmax <- col_max[j]
}
}
# Step 7: Find MaxMin (Maximum of row minimums)
maxmin <- row_min[1]
for(i in 2:r1) {
if(row_min[i] > maxmin) {
maxmin <- row_min[i]
}
}
cat("MinMax =", minmax, "\n")
cat("MaxMin =", maxmin, "\n")
# Step 8: Check equality
if(minmax == maxmin) {
cat("MinMax and MaxMin are Equal (Saddle Point Exists)\n")
} else {
cat("MinMax and MaxMin are Not Equal\n")
}
v : Vector containing the raw input values.m : Matrix formed iteratively from the vector.r1 / c1 : Number of rows and columns in the matrix.row_min : Array storing the minimum value of each row.col_max : Array storing the maximum value of each column.minmax : The minimum value among the computed column maximums.maxmin : The maximum value among the computed row minimums.Based on the questions in this year's examination, aspirants should heavily focus on the following domains:
&, |, ^, ~) and shift operators is crucial. The examiner expects clarity on how these operate at the memory level.for loop).fopen, fclose, fprintf, fscanf) remain a high-yield topic for theory and short coding questions.max(), min(), or sort().(float)i/j) to prevent unintended integer division leading to zero or truncated results.To solidify the concepts learned in this paper, attempt the following questions using the exact same constraints (manual logic, no built-in functions):
Write a C program to check if a given positive integer is a power of 2 using only bitwise operators (no loops or division).
(n & (n - 1)) == 0 is the key.
Write an R script to compute the transpose of a given $m \times n$ matrix. You must create an empty matrix of dimensions $n \times m$ and use nested loops to populate it. Do not use the t() function.
nrow() / ncol(). Initialize a new matrix $B$ filled with zeros, where the rows of $B$ equal the columns of $A$, and the columns of $B$ equal the rows of $A$. Use two nested loops for (i in 1:m) and for (j in 1:n), and assign the values using the logic: B[j, i] <- A[i, j].