help me in accessing bits

Please support our C advertiser: Programming Forums - DaniWeb Sister Site
Reply

Join Date: Sep 2004
Posts: 6
Reputation: raar is an unknown quantity at this point 
Solved Threads: 0
raar raar is offline Offline
Newbie Poster

help me in accessing bits

 
0
  #1
Sep 23rd, 2004
Hi,
I want a generate a node that consists of three integers but I want to store these three integers in bits and not bytes
e.g if the node has the values 4,3,2
in integers it will be stored as 00000100-00000011-00000010
but if I store them in bits then they will be 01001110 occupy 1 byte instead of three
taking into consideration that the values are undetermined in the compilation time(I can’t use byte: x 4)
can u tell me how can I do such a thing and how can I dump them as 4,3& 2 again
thx for time
Reply With Quote Quick reply to this message  
Join Date: Apr 2004
Posts: 4,398
Reputation: Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future 
Solved Threads: 245
Team Colleague
Dave Sinkula's Avatar
Dave Sinkula Dave Sinkula is offline Offline
long time no c

Re: help me in accessing bits

 
0
  #2
Sep 23rd, 2004
#include <stdio.h>
 #include <limits.h>
 
 struct type /* note: bit order is implementation-dependent */
 {
    unsigned int a : 2;
    unsigned int b : 2;
    unsigned int c : 4;
 };
 
 void showbits(unsigned char byte)
 {
    unsigned char bit;
    for ( bit = 1 << (CHAR_BIT - 1); bit; bit >>= 1 )
    {
 	  putchar(byte & bit ? '1' : '0');
    }
    putchar('\n');
 }
 
 int main()
 {
    struct type value = {2,3,4};
    printf("sizeof value = %d\n", (int)(sizeof value));
    printf("value.a = %u\n", value.a);
    printf("value.b = %u\n", value.b);
    printf("value.c = %u\n", value.c);
    showbits(*(unsigned char*)&value);
    return 0;
 }
 
 /* my output
 sizeof value = 1
 value.a = 2
 value.b = 3
 value.c = 4
 01001110
 */
"One of the methods used by statists to destroy capitalism consists in establishing controls that tie a given industry hand and foot, making it unable to solve its problems, then declaring that freedom has failed and stronger controls are necessary." --Ayn Rand
Reply With Quote Quick reply to this message  
Join Date: Sep 2004
Posts: 185
Reputation: Stack Overflow is an unknown quantity at this point 
Solved Threads: 4
Stack Overflow's Avatar
Stack Overflow Stack Overflow is offline Offline
C Programmer

Re: help me in accessing bits

 
0
  #3
Sep 23rd, 2004
Greetings,

Binary operations can be somewhat confusing at times, though quite simple to understand. It is customary to refer to a digit in binary (either 1 or 0) as a bit, likewise a byte is a set of eight bits. So, for example, 10100110 is a byte. A "nibble" is half of a byte, as it is a binary number with four bits.

A word is a sixteen-bit binary number. It is equivalent to two bytes, or four nibbles. A double word is a 32-bit binary number, so a dword is equivalent to two words, or four bytes, or eight nibbles. Most commonly, the number of bits (or bytes) in a number-type is referred to as that type's width.

Bit-type Examples:
  1. Bit 1 bin
  2. Nibble 0110 bin
  3. Byte 00100101 bin
  4. Word 1110100001010110 bin
  5. D Word 10100110000111101110111011010101 bin

More information about Binary and its essentials can be found here.


- Stack Overflow
Following the rules will ensure you get a prompt answer to your question. If posting code, please include BB [code][/code] tags. Your question may have been asked before, try the search facility.

IRC
Channel: irc.daniweb.com
Room: #c, #shell
Reply With Quote Quick reply to this message  
Join Date: Apr 2004
Posts: 4,398
Reputation: Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future 
Solved Threads: 245
Team Colleague
Dave Sinkula's Avatar
Dave Sinkula Dave Sinkula is offline Offline
long time no c

Re: help me in accessing bits

 
0
  #4
Sep 23rd, 2004
This is perhaps a more portable way than my previous post.
 #include <stdio.h>
 #include <limits.h>
 
 void showbits(unsigned char byte)
 {
    unsigned char bit;
    for ( bit = 1 << (CHAR_BIT - 1); bit; bit >>= 1 )
    {
 	  putchar(byte & bit ? '1' : '0');
    }
    putchar('\n');
 }
 
 unsigned char pack(unsigned char a, unsigned char b, unsigned char c)
 {
    return ((a & 0xF) << 4) | ((b & 0x3) << 2) | (c & 0x3);
 }
 
 void unpack(unsigned char value, unsigned char *a, unsigned char *b, unsigned char *c)
 {
    *a = (value >> 4) & 0xF;
    *b = (value >> 2) & 0x3;
    *c =  value	   & 0x3;
 }
 
 int main()
 {
    unsigned char a, b, c, value = pack(4,3,2);
    printf("sizeof value = %d\n", (int)(sizeof value));
    showbits(value);
    unpack(value, &a, &b, &c);
    printf("a = %d, b = %d, c = %d\n", a, b, c);
    return 0;
 }
 
 /* my output
 sizeof value = 1
 01001110
 a = 4, b = 3, c = 2
 */
"One of the methods used by statists to destroy capitalism consists in establishing controls that tie a given industry hand and foot, making it unable to solve its problems, then declaring that freedom has failed and stronger controls are necessary." --Ayn Rand
Reply With Quote Quick reply to this message  
Join Date: Apr 2004
Posts: 4,398
Reputation: Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future 
Solved Threads: 245
Team Colleague
Dave Sinkula's Avatar
Dave Sinkula Dave Sinkula is offline Offline
long time no c

Re: help me in accessing bits

 
0
  #5
Sep 23rd, 2004
Originally Posted by Stack Overflow
Binary operations can be somewhat confusing at times, though quite simple to understand. It is customary to refer to a digit in binary (either 1 or 0) as a bit, likewise a byte is a set of eight bits. So, for example, 10100110 is a byte. A "nibble" is half of a byte, as it is a binary number with four bits.

A word is a sixteen-bit binary number. It is equivalent to two bytes, or four nibbles. A double word is a 32-bit binary number, so a dword is equivalent to two words, or four bytes, or eight nibbles. Most commonly, the number of bits (or bytes) in a number-type is referred to as that type's width.
In C and C++ these identities do not necessarily hold. There can be a 16-bit byte and a 1-byte int, and other such configurations.
"One of the methods used by statists to destroy capitalism consists in establishing controls that tie a given industry hand and foot, making it unable to solve its problems, then declaring that freedom has failed and stronger controls are necessary." --Ayn Rand
Reply With Quote Quick reply to this message  
Join Date: Sep 2004
Posts: 6
Reputation: raar is an unknown quantity at this point 
Solved Threads: 0
raar raar is offline Offline
Newbie Poster

Re: help me in accessing bits

 
0
  #6
Sep 23rd, 2004
Originally Posted by Dave Sinkula
This is perhaps a more portable way than my previous post.
 #include <stdio.h>
 #include <limits.h>
 
 void showbits(unsigned char byte)
 {
    unsigned char bit;
    for ( bit = 1 << (CHAR_BIT - 1); bit; bit >>= 1 )
    {
 	  putchar(byte & bit ? '1' : '0');
    }
    putchar('\n');
 }
 
 unsigned char pack(unsigned char a, unsigned char b, unsigned char c)
 {
    return ((a & 0xF) << 4) | ((b & 0x3) << 2) | (c & 0x3);
 }
 
 void unpack(unsigned char value, unsigned char *a, unsigned char *b, unsigned char *c)
 {
    *a = (value >> 4) & 0xF;
    *b = (value >> 2) & 0x3;
    *c =  value	   & 0x3;
 }
 
 int main()
 {
    unsigned char a, b, c, value = pack(4,3,2);
    printf("sizeof value = %d\n", (int)(sizeof value));
    showbits(value);
    unpack(value, &a, &b, &c);
    printf("a = %d, b = %d, c = %d\n", a, b, c);
    return 0;
 }
 
 /* my output
 sizeof value = 1
 01001110
 a = 4, b = 3, c = 2
 */
thx for ur help, but all this is under the assumption that the first char must be packed in 4 bits, the second in 2bits,.. but what if the sizes increase ,what about if i want to write 200,400,3 in bits how can i do so
Reply With Quote Quick reply to this message  
Join Date: Apr 2004
Posts: 4,398
Reputation: Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future 
Solved Threads: 245
Team Colleague
Dave Sinkula's Avatar
Dave Sinkula Dave Sinkula is offline Offline
long time no c

Re: help me in accessing bits

 
0
  #7
Sep 23rd, 2004
Originally Posted by raar
thx for ur help, but all this is under the assumption that the first char must be packed in 4 bits, the second in 2bits,.. but what if the sizes increase ,what about if i want to write 200,400,3 in bits how can i do so
Well, if you want to store a nine-bit value (400) in an eight-bit object (the common unsigned char), you are going to have issues now, aren't you?
"One of the methods used by statists to destroy capitalism consists in establishing controls that tie a given industry hand and foot, making it unable to solve its problems, then declaring that freedom has failed and stronger controls are necessary." --Ayn Rand
Reply With Quote Quick reply to this message  
Join Date: Sep 2004
Posts: 6
Reputation: raar is an unknown quantity at this point 
Solved Threads: 0
raar raar is offline Offline
Newbie Poster

Re: help me in accessing bits

 
0
  #8
Sep 24th, 2004
Originally Posted by Dave Sinkula
Well, if you want to store a nine-bit value (400) in an eight-bit object (the common unsigned char), you are going to have issues now, aren't you?
yeah it looks so , but how can i solve such a problem, i'm sure there is a solution
actually i can check for the long of the number in binary and make shifting the number dynamically but in such away the problem will be in retreiving the numbers back .
what do u think
Reply With Quote Quick reply to this message  
Join Date: Apr 2004
Posts: 4,398
Reputation: Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future 
Solved Threads: 245
Team Colleague
Dave Sinkula's Avatar
Dave Sinkula Dave Sinkula is offline Offline
long time no c

Re: help me in accessing bits

 
0
  #9
Sep 24th, 2004
Do your values have a known fixed range? Then the solutions I presented can be modified.

Or are you trying to compress data? Then search for compression algorithms.

Or can each value be of a different size? ASN.1 and BER is a way to handle that one.

What is the "big picture"?
"One of the methods used by statists to destroy capitalism consists in establishing controls that tie a given industry hand and foot, making it unable to solve its problems, then declaring that freedom has failed and stronger controls are necessary." --Ayn Rand
Reply With Quote Quick reply to this message  
Join Date: Sep 2004
Posts: 6
Reputation: raar is an unknown quantity at this point 
Solved Threads: 0
raar raar is offline Offline
Newbie Poster

Re: help me in accessing bits

 
0
  #10
Sep 24th, 2004
Originally Posted by Dave Sinkula
Do your values have a known fixed range? Then the solutions I presented can be modified.

Or are you trying to compress data? Then search for compression algorithms.

Or can each value be of a different size? ASN.1 and BER is a way to handle that one.

What is the "big picture"?
actually i'm trying to compress data but i can't find an answer in compression algorithms
values are of no fixed size ,they can be anything but one of them muct always take 7bits(unsigned char)the other two must be packed in bits
so what do u think???????
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:



Other Threads in the C Forum
Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC