This is a C Program takes byte as input and returns all the bits between given positions.
This C Program takes byte as input and returns all the bits between given positions.
Take input from the user and returns positions of a and b as shown in the program below.
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; }
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.
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.
Here’s the list of Best Books in C Programming, Data-Structures and Algorithms
- Apply for C Internship
- Check C Books
- Check Computer Science Books
- Apply for Computer Science Internship
- Practice BCA MCQs