C Program to Read Byte and Return All Bits

«
»

This is a C Program takes byte as input and returns all the bits between given positions.

Problem Description

This C Program takes byte as input and returns all the bits between given positions.

Problem Solution

Take input from the user and returns positions of a and b as shown in the program below.

Program/Source Code

Here is source code of the C Program to take byte as input and returns all the bits between given positions. The C program is successfully compiled and run on a Linux system. The program output is also shown below.

/*
 * C Program takes Byte as Input and returns all the Bits between 
 * given Positions
 */
#include <stdio.h>
#include <string.h>
 
int main (int argc, char *argv[])
{
	int a = 0, b = 0, temp = 0, i = 0, countCast = 0;
 
	char BYTE_HERE[8];
	int FULL_BYTE[8];
 
	printf ("Enter the BYTE:  \n");
	gets(BYTE_HERE);
 
	if (strlen (BYTE_HERE) < 8) {
		printf ("Enter a full 8-bit value.\n");
		return 0;
	}
 
	printf ("\nEnter the positions a and b : \n");
	scanf ("%d %d", &a, &b);
 
	// copy from character array to integer array
	for (i = 0; i <	 8; i++)
	{
			// convert the character to integer
			FULL_BYTE[i] = BYTE_HERE[i] - '0';
 
	}
 
	// just print the bits
	for (i = a; i <= b; i++)
	{
		printf ("%d ", FULL_BYTE[i]);
	}
	printf("\nBits between positions a and b are:\n");
 
	return 0;
}
Program Explanation

1. In this C program, fist we define one integer array and one character array. And also take the input from the user.
2. If statement is used to check whether the input is 8 bit in length or not if the input is less than 8 bit inform and abort.
3. For statement is used to copy each bit from character array and convert it to an integer. FULL_BYTE[i] = BYTE_HERE[i] – ‘0’; is used to convert the character to integer
4. Print the bits by using the print statement.

Sanfoundry Certification Contest of the Month is Live. 100+ Subjects. Participate Now!
advertisement
advertisement
Runtime Test Cases

Test case 1 – Invalid Input

Enter the BYTE:
101
Enter a full 8-bit value.

Test case 2 – Here, the entered positions a and b are 0 & 5

Enter the BYTE: 
10101010
Enter the positions a and b : 
0  
5
 
Bits between positions a and b are: 1 0 1 0 1 0

Test case 3 – Here, the entered positions a and b are 0 & 3

Enter the BYTE: 
11110000
Enter the positions a and b : 
0  
3
 
Bits between positions a and b are: 1 1 1 1

Sanfoundry Global Education & Learning Series – 1000 C Programs.

advertisement

Here’s the list of Best Books in C Programming, Data-Structures and Algorithms

If you wish to look at programming examples on all topics, go to C Programming Examples.

advertisement

advertisement
advertisement
Subscribe to our Newsletters (Subject-wise). Participate in the Sanfoundry Certification contest to get free Certificate of Merit. Join our social networks below and stay updated with latest contests, videos, internships and jobs!

Youtube | Telegram | LinkedIn | Instagram | Facebook | Twitter | Pinterest
Manish Bhojasia - Founder & CTO at Sanfoundry
Manish Bhojasia, a technology veteran with 20+ years @ Cisco & Wipro, is Founder and CTO at Sanfoundry. He lives in Bangalore, and focuses on development of Linux Kernel, SAN Technologies, Advanced C, Data Structures & Alogrithms. Stay connected with him at LinkedIn.

Subscribe to his free Masterclasses at Youtube & technical discussions at Telegram SanfoundryClasses.