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
raar
0
Newbie Poster
Recommended Answers
Jump to Post#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); …
Jump to PostGreetings,
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" …
Jump to PostThis 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'); …
All 9 Replies
Dave Sinkula
2,398
long time no c
Team Colleague
Stack Overflow
8
Junior Poster
Dave Sinkula
2,398
long time no c
Team Colleague
Dave Sinkula
2,398
long time no c
Team Colleague
raar
0
Newbie Poster
Dave Sinkula
2,398
long time no c
Team Colleague
raar
0
Newbie Poster
Dave Sinkula
2,398
long time no c
Team Colleague
raar
0
Newbie Poster
Be a part of the DaniWeb community
We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.