Go4Expert (http://www.go4expert.com/)
-   C (http://www.go4expert.com/forums/c/)
-   -   what is bit field? (http://www.go4expert.com/forums/bit-field-t1937/)

neha 18Nov2006 22:35

what is bit field?

shabbir 20Nov2006 10:48

Re: what is bit field?
Moved thread from Introduce yourself forum to C-C++ forum

Bit field
A member of a structure or union that contains a specified number of bits.

pradeep 20Nov2006 10:54

Re: what is bit field?
A bit field is a common idiom used in computer programming to store a set of Boolean datatype flags compactly, as a series of bits. The bit field is stored in an integral type of known, fixed bit-width. Each Boolean flag is stored in a separate bit. Usually the source code will define a set of constants, each a power of two, that semantically associate each individual bit with its respective Boolean flag. The bitwise operators AND, OR, and NOT are used in combination to set, reset and test the flags.

A bit field is distinguished from a bit array in that the latter is used to store a large set of bits indexed by integers and is often wider than any integral type supported by the language. Bit fields, on the other hand, typically fit within a machine word, and the denotation of bits is independent of their numerical index.

ranims 26Jun2007 15:17

Re: what is bit field?
i am storing a number in long int, i.e.it is occuping 4 bytes.i am reading each byte data into a character array seperately. now by using those characters,how to to get the original value .

ex: number :3456789
Binary format is: 11 0100 1011 1111 0001 0101
c[0]=0000 0000=    0
c[1]=0011 0100=  52
c[2]=1011 1111= 191
c[3]=0001 0101=  21

by using above chars how to get original value i.e. 3456789

DaWei 26Jun2007 16:24

Re: what is bit field?
You have to be aware of the endianness of your particular implementation.

ranims 26Jun2007 16:41

Re: what is bit field?
Thanks a lot

How to read the data from that string

        long int li,num;
        unsigned char c[4],temp;
        int i,s;
        printf("\nEnter any number: ");
        printf("\nByte1 : %ld",li&0xFF);
        printf("\nByte2 : %ld",(li>>8)&0xFF);
        printf("\nByte3 : %ld",(li>>16)&0xFF);
                printf("\nByte4 : %ld",(li>>24)&0xFF);
            printf("\nactual: %d",c[i]);

                printf("\nactual1: %d",c[i]);

        //printf("\noriginal: %ld",((c[0]>>24)&0x000000FF) |((c[1]>>8)&0x00FF)|((c[2]<<8)  &0xFF00)|((c[3]<<24)&0xFF000000));


My requirement is by reading character array i should get original value which i have splitted

DaWei 26Jun2007 17:43

Re: what is bit field?
There is no way to comment on your code without discussing endianness, as I mentioned. The bytes of the integer will be stored in one order on a Intel machine, another order on a Motorola machine, and perhaps even another order on another machine.

Consider, for example, the number, 4660. In binary that would be (expressed with the most significant bit to the left) 0001 0010 0011 0100. In hex, it would be 0x1234. If you store that number at address 0 on an Intel machine, the contents of locations 0-3 will be, in hex, 34 12 00 00.

On a 32-bit Motorola machine that would be 00 00 12 34. In other words, the Intel machine stores the "little end" first. This is called little endian. The Motorola machine is big endian. It stores the big (most significant) end first.

The processors are built differently in how they address memory when they store the contents of an internal register.

If you know the type of machine you have (you don't mention it in the post), then you can reconstruct the value by reassembling them in the order approprate to your endianness.

This can be an issue when exchanging data over a network in byte streams. The network is big endian. Your library will provide functions called hton and ntoh (or similar). These translate the host to network order and vice versa. For a big endian machine, these will be empty functions, because the order is already correct. For an Intel machine, these functions will correctly reorder the bytes.

All times are GMT +5.5. The time now is 10:55.