0% found this document useful (0 votes)
30 views7 pages

1B - Lab 01 - CSE 4202 2023-24 Basics and Bitwise Operators

Uploaded by

bjennie055
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
30 views7 pages

1B - Lab 01 - CSE 4202 2023-24 Basics and Bitwise Operators

Uploaded by

bjennie055
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 7

CSE 4202-Structured Programming-II Lab Summer 2023-24

Lab-01
Date: May 15, 2025 (Thursday)
Target Group: 1B
Topic: Warm-up tasks, Bitwise operations

Instructions:
-​ Task naming format: fullID_T01L01_1B.c
-​ If you find any issue in the problem descriptions/test cases,
comment in the Google Classroom.
-​ Please comment if you find any tricky test cases that were not
included and others might forget to handle.
-​ Modified portions will be marked in BLUE color.

Distribution:

Group Lab Task

1A 1, 2, 3, 4, 5
Task 1:

Write a C program for checking if a number is odd or even without


using the "mod (%)" and "division (/)" operators. Print ‘odd’ for odd
numbers and ‘even’ for even numbers.

Sample Input Sample Output

151 odd

23374 even

23 odd
Task 2:

In simple terms, ‘Encrypting’ a text means alternating the characters


in such a way so that it is not readily readable. One or more keys are
associated with performing such encryption and then converting it back
to meaningful format (decryption). In simple terms, encryption is like
a lock, which requires a key to be unlocked.

A straightforward way to encrypt a string is by bitwise XORing every


character of the string with a ‘key character’. For instance, if the
sentence is “Hello World” and the key is ‘&’, we will perform XOR of
‘H’ with ‘&’ first, then ‘e’ with ‘&’ and so on. The characters will
be stored back in their original positions in the array. The
simplicity of this encryption is that the encrypted string can be
re-encrypted (aka decryption) with the same key, ‘&’ in our case, to
get back the original string.

Now, write a program with a function that takes a string (char *) and
key (char) as input and encrypts/decrypts them in place. The
declaration of the function should look like below.

void xorCrypt(char * str, char key);

Underlined text in inputs is printed from the programs.

Sample Input Sample Output

Enter a sentence: Hello World Encrypted: nCJJI♠qITJB


Enter a key: & Decrypted: Hello World

Enter a sentence: To C or not to C Encrypted: ↕)f♣f)4f()2f2)f♣


Enter a key: F Decrypted: To C or not to C

This naive approach has some caveats; for instance, the output can
break if a character is converted to some ‘escape’ characters. There
are many ways to fix it, but it is not required for this task. Also,
based on how your operating system and the C compiler deal with
characters, the encrypted format might look a bit different.
Task 3:

Have you ever wondered whether we can find how many bits a specific
data type (mainly integral types) contains without relying on the
compiler functions, like ‘sizeof’? There is a solution!

We can take an unsigned data type, e.g. ‘unsigned int’, and then set
the value to 0. We can then perform a bitwise complement (~) to set
all the bits to 1. Then we can keep on consecutively shifting the bits
either left or right until the number becomes 0. We can keep track of
the number of shifts required, which will be the total bit count.

This method only works appropriately with unsigned integral data types
because some bitwise operations are implementation-dependent for
signed types. In contrast, floating-point numbers are stored and read
from memory using a different format.

Now write a program that would print the bit count of the following
data types in C.

●​ unsigned char
●​ unsigned short
●​ unsigned int
●​ unsigned long
●​ unsigned long long

Sample Input Sample Output

unsigned char: 8
unsigned short: 16
unsigned int: 32
unsigned long: 32
unsigned long long: 64

The values, in some cases, might be system dependent, and you might
get slightly different values from some data types.
Task 4: (Swapping numbers with Bitwise operation)

Till now, you have been familiarized with two main methods of swapping
the values of two variables, the temporary variable method and the
arithmetic method. There is another method where we can use bitwise
XOR(^) to swap the values of two variables. This method relies on
finding the bitwise difference between the two numbers and then
swapping the values.

One unfortunate downside is, we can’t directly use this method with
floating-point numbers. (But there is a workaround using unions or
type punning, but that is not the concern for this lab.)

Write a program that would take two integer numbers as input and swap
the values of the two integers using the ‘bitwise XOR operation’.

Sample Input Sample Output

10 100 100 10

10 -100 -100 10

-10 100 10 -100

-10 -100 -100 -10


Task 5: Haunted Room

Mr. C. Pat Rayban lives in a haunted room at the dormitory of Imperial


University of Texas (IUT). His room contains 𝑁 switches, connected to
different electrical devices, such as lights, fans, washing machines,
telephones, etc. However, there are different types of wiring problems that
make the devices behave weirdly. The problem types are given next:

●​ Type 1: If any two devices are facing type 1 problem, basically they
are in a potential overload condition and turning both of them on
together would set off both. Turning on any single device is fine.
●​ Type 2: If any two devices are in type 2 problem, it means their
internal wirings have melted together. Unless you turn both of them on,
none of the devices get electricity and will not turn on. So, all or
nothing.
●​ Type 3: If any single device is facing type 3 problem, it means its
switch is wrongly wired and it is dependent on some other device. If
the other device is turned on, this one will be turned off. And if the
other one is turned off, this one will be turned on.

What makes Mr. Rayban’s room haunted is not just the problem with the wiring,
but the fact that all of these take place 4-5 minutes after switches have
been pressed in his room.

Now write a program in C that would first take an integer, 𝑁 (2 ≤ 𝑁 ≤ 32),


the number of switches as input, followed by the initial state of the 𝑁
switches, where 0 depicts ‘off’ and 1 depicts ‘on’ state. Then it will take
another integer, 𝑀 (0 ≤ 𝑀 ≤ 8𝑁), the number of problems in the room as
input, followed by the problems. Each problem will be stated using 3
consecutive integers, first the problem type, then the two devices associated
with it. In the case of a type 3 problem, the second integer would be the
dependent device and the third one would be the device on which it depends.
Consider that the devices are numbered from 0 to 𝑁 − 1 (left to right). The
problems will be resolved in the order they are written in the input. Now,
given the inputs, your program will produce the final state of the switches
after the problem occurs and also print out the number of active devices in
the end.

You need to use bitwise operators up to a significant amount to solve the


problem. You CANNOT use arrays.
Sample Execution

Sample Input Sample Output

8 0 0 0 0 1 0 1 1
1 0 1 0 1 1 0 1 3
3
1 0 2
2 3 5
3 6 1

13 0 0 1 1 1 0 1 0 1 0 0 0 0
1 0 1 0 1 0 1 0 1 0 1 1 1 5
5
1 11 12
1 9 10
2 10 11
2 0 3
3 3 0

5 1 0 1 1 0
1 0 1 1 0 3
3
1 3 4
2 2 3
3 3 4

In the first sample, states of 8 devices are given and there are 3 problems.
First one is of type one which links device 0 and 2. Since 0 and 2 both are
on, it will cause overload and both of them will turn off. So, the new state
would be, {0 0 0 0 1 1 0 1}. Then the second problem is of type 2, linking
devices 3 and 5. Since 3 is off and 5 is on, both of them would turn off,
resulting in the new arrangement {0 0 0 0 1 0 0 1}. Finally, the third one
is type 3 problem where device 6 depends on device 1. Since device 1 is off
now, device 6 will be turned on, giving the final configuration of
{0 0 0 0 1 0 1 1}.

●​

You might also like