C

Topic: Arrays

Explain about Bit Arrays ?

If in a program a variable is to take only two values 1 and 0, we really need only a single bit to store it. Similarly, if a variable is to take values from 0 to 3, then two bits are sufficient to store these values. And if a variable is to take values from 0 through 7, then three bits will be enough, and so on. Why waste an entire integer when one or two or three bits will do? Because there aren't any one bit or two bit or three bit data types available in C. However, when there are several variables whose maximum values are small enough to pack into a single memory location, we can use `bit fields' to store several values in a single integer. Bit fields are discussed in most standard C texts. They are usually used when we want to store assorted information which can be accommodated in 1, 2, 3 bits etc.For example, the following data about an employee can be easily stored using bit fields.male or femalesingle, married, divorced or widowedhave one of the eight different hobbiescan choose from any of the fifteen different schemes proposed by the company to pursue his/her hobby.This means we need one bit to store gender, two to store marital status, three for hobby, and four for scheme (with one value used for those who are not desirous of availing any of the schemes). We need ten bits altogether, which means we can pack all this information into a single integer, since an integer is 16 bits long.At times we may need to store several True or False statuses. In such cases instead of using bit fields using an array of bits would be more sensible. On this array we may be required to perform the following operations:Set a bit (make it 1).Clear a bit (make it 0).Test the status of a bit in the array.Reach the appropriate bit slot in the array.Generate a bit mask for setting and clearing a bit.We can implement these operations using macros given below:#define CHARSIZE 8#define MASK ( y ) ( 1 << y % CHARSIZE )#define BITSLOT ( y ) ( y / CHARSIZE )#define SET ( x, y ) ( x[BITSLOT( y )] |= MASK( y ) )#define CLEAR ( x, y ) ( x[BITSLOT( y )] &= ~MASK( y ) )#define TEST ( x, y ) ( x[BITSLOT( y )] & MASK( y ) )#define NUMSLOTS ( n ) ( ( n + CHARSIZE - 1) / CHARSIZE )Using these macros we can declare an array of 50 bits be saying,char arr[NUMSLOTS(50)] ;To set the 20th bit we can say,SET(arr, 20 ) ;And if we are to test the status of 40th bit we may say,if ( TEST ( arr, 40 ) )Using bit arrays often results into saving a lot of precious memory. For example, the following program which implements the Sieve of Eratosthenes for generating prime numbers smaller than 100 requires only 13 bytes. Had we implemented the same logic using an array of integers we would have required an array of 100 integers, that is 200 bytes.#include <stdio.h>#include <string.h>#define MAX 100main( ){char arr[NUMSLOTS( MAX )] ;int i, j ;memset ( arr, 0, NUMSLOTS( MAX ) ) ;for ( i = 2 ; i < MAX ; i++ ){if ( !TEST ( arr, i ) ){printf ( "\n%d", i ) ;for ( j = i + i ; j < MAX ; j += i )SET ( arr, j ) ;}}}

Browse random answers:

Do array subscripts always start with zero?
What will happen if in a C program you assign a value to an array element whose subscript exceeds the size of array?
What is the difference between null array and an empty array?
How to remove duplicate elements from an array?
Why is it necessary to give the size of an array in an array declaration?
Array is an lvalue or not?
Does mentioning the array name gives the base address in all the integers? 
what will happen if we try to store more number of elements than specified in array size? 
What is an array?
How to print 2 dimensional array in descending order?
What is Jagged Array?
Write a program to delete an element from an array?
Are arrays value types or reference types?
Assume you have an array that contains a number of strings (perhaps char * a[100]). Each string is a word from the dictionary. Your task, described in high-level terms, is to devise a way to determine and display all of the anagrams within the array (two words are anagrams if they contain the same characters; for example, tales and slate are anagrams.)
How can you sort the elements of the array in descending order? 
What are the 3 different types of arrays?
When does the compiler not implicitly generate the address of the first element of an array?
Is it possible to have negative index in an array?
Difference between arrays and linked list?
Why is it necessary to give the size of an array in an array declaration?
What is an array of pointers?
What is an array of pointers?
Why is it necessary to give the size of an array in an array declaration?
Can the sizeof operator be used to tell the size of an array passed to a function? 
How would you use qsort() function to sort the name stored in an array of pointers to string? 
Are pointers really faster than arrays? 
How can you sort the elements of the array in descending order? 
How to find the row and column dimension of a given 2-D array?
Data structuresHow to distinguish between a binary tree and a tree?Allocating memory for a 3-D array#include "alloc.h"#define MAXX 3#define MAXY 4#define MAXZ 5main( ){int ***p, i, j, k ;p = ( int *** ) malloc ( MAXX * sizeof ( int ** ) ) ;for ( i = 0 ; i < MAXX ; i++ ){p[i] = ( int ** ) malloc ( MAXY * sizeof ( int * ) ) ;for ( j = 0 ; j < MAXY ; j++ )p[i][j] = ( int * ) malloc ( MAXZ * sizeof ( int ) ) ;}for ( k = 0 ; k < MAXZ ; k++ ){for ( i = 0 ; i < MAXX ; i++ ){for ( j = 0 ; j < MAXY ; j++ ){p[i][j][k] = i + j + k ;printf ( "%d ", p[i][j][k] ) ;}printf ( "\n" ) ;}printf ( "\n\n" ) ;}}
How does C compiler store elements in a multi-dimensional array?
Explain about Variably Dimensioned Arrays ?
Explain about Bit Arrays ?