Byte Class

Learn to implement two overloaded setBit( ) and clearBit( ) methods.

Challenge

Write a program that contains a Byte class and overloaded setBit( ) and clearBit( ) functions as shown below.

setBit ( num, 3 ) - Sets 1 in bit number 3 in num

setBit ( num, 2, 5 ) - Sets 1 in bits 2 to 5 in num

clearBit ( num, 2 ) - Clears 0 in bit number 2 in num

clearBit ( num, 3, 6 ) - Clears 0 in bits 3 to 6 in num

Sample run

Here’s what you should see when you run the program.

Initial value: Character Value: 01000001
Value after setting bit number 4: Character Value: 01010001
Value after setting bits 1 to 3: Character Value: 01011111
Value after clearing bit number 2: Character Value: 01011011
Value after clearing bits 4 to 6: Character Value: 00001011

Coding exercise

Try to solve this challenge on your own. If you can’t solve it, you can look at the solution and explanation below.

Get hands-on with 1200+ tech skills courses.