value of bit

L

Lazyworm

Hi there

given a primitive type variable, such as "char" or "int", how do i get
the value of each bit in the char? or, how do i get the binary form of
the variable?

Thanks a lot

Lazyworm
 
W

Walter Roberson

Lazyworm said:
given a primitive type variable, such as "char" or "int", how do i get
the value of each bit in the char? or, how do i get the binary form of
the variable?

int somevariable = 123;
unsigned char varp = (unsigned char *)&somevariable;
Then varp[0] to varp[sizeof(somevariable)-1] are unsigned char
that can be used to access the individual bytes of the value.

Note the use of unsigned char, not of char. unsigned char is
guaranteed not to have any trap values or padding bits: provided
that the underlying data representation really is binary
(not, for example, trinary on some bizarre computer) then
unsigned char is the only type that you can use to be sure that you
can access the individual bits.

But be careful: varp[0] does not necessarily contain the
"most significant byte" of the value you wish to examine, and
it does not necessarily contain the "least significant byte" either:
varp[0] could end being something in the "middle" of how that
particular systems treats "int" values (seriously!). And there
can be padding bits in numeric values, and there can be padding
bits in structures that use bitfields, and there can be padding
bytes in structures -- so although you might be able to examine
the underlying bytes, it might not always be immediately clear what
those bytes *mean*.
 
W

Willem

Walter Roberson wrote:
) In article <18d7ec2d-c1f7-487a-8c8c-b53eb8e3e5b4@l64g2000hse.googlegroups.com>,
)
)>given a primitive type variable, such as "char" or "int", how do i get
)>the value of each bit in the char? or, how do i get the binary form of
)>the variable?
)
) int somevariable = 123;
) unsigned char varp = (unsigned char *)&somevariable;
) Then varp[0] to varp[sizeof(somevariable)-1] are unsigned char
) that can be used to access the individual bytes of the value.

NB: The OP wanted bits, not bytes.

Furthermore, using shift and mask operations is much more portable, and
any compiler worth its salt will optimize it to similar performance anyway.


SaSW, Willem
--
Disclaimer: I am in no way responsible for any of the statements
made in the above text. For all I know I might be
drugged or something..
No I'm not paranoid. You all think I'm paranoid, don't you !
#EOT
 
W

Walter Roberson

Walter Roberson wrote:
) In article <18d7ec2d-c1f7-487a-8c8c-b53eb8e3e5b4@l64g2000hse.googlegroups.com>,
) Lazyworm <[email protected]> wrote:
)>given a primitive type variable, such as "char" or "int", how do i get
)>the value of each bit in the char? or, how do i get the binary form of
)>the variable?
) int somevariable = 123;
) unsigned char varp = (unsigned char *)&somevariable;
) Then varp[0] to varp[sizeof(somevariable)-1] are unsigned char
) that can be used to access the individual bytes of the value.
NB: The OP wanted bits, not bytes.

The original pointer wanted "the binary form of the variable".
Furthermore, using shift and mask operations is much more portable,

shift and mask operations on the original data type cannot give
you the "binary form of the variable", as shift and mask operations
only deal with values, not with binary forms. In order to get raw
binary to apply the shift and mask operations -to-
in order to extract individual bit values, you need to examine
the variable by way of unsigned char.
and
any compiler worth its salt will optimize it to similar performance anyway.

If your int has a couple of padding bits in it, how are you going
to see "the binary form" of those padding bits using shifts and masks
applied to the int ? The compiler cannot "optimize to similar performance"
that which cannot be expressed.

Using shift and masks will also not allow you to investigate
points such as what the "byte order" is for the int -- an aspect
which is not portable from system to system, but is consistent
for any one system (unless the user does Strange Things to change
it on one of the few processor families that allow it to be changed.)
The "byte order" is part of the "binary form of the variable",
at least as I would interpret the phrase "binary form of the variable".
 
V

vippstar

#define STRING "%15f = %s\n"
#define E_TYPE float
#define INITIAL (1.0f / 3)
#define FINAL 50
#define INC(E) ((E) *= 2)

typedef E_TYPE e_type;

<snip>

Aren't identifiers starting with E reserved? Or that's only if you
include <errno.h>?
 
K

Keith Thompson

Dann Corbit said:
Lazyworm said:
Hi there

given a primitive type variable, such as "char" or "int", how do i get
the value of each bit in the char? or, how do i get the binary form of
the variable?

20.8: How can I implement sets or arrays of bits?

A: Use arrays of char or int, with a few macros to access the
desired bit at the proper index. Here are some simple macros to
use with arrays of char:

#include <limits.h> /* for CHAR_BIT */

#define BITMASK(b) (1 << ((b) % CHAR_BIT))
#define BITSLOT(b) ((b) / CHAR_BIT)
#define BITSET(a, b) ((a)[BITSLOT(b)] |= BITMASK(b))
#define BITTEST(a, b) ((a)[BITSLOT(b)] & BITMASK(b))

(If you don't have <limits.h>, try using 8 for CHAR_BIT.)

References: H&S Sec. 7.6.7 pp. 211-216.

Note that the "If you don't have <limits.h>" clause is very unlikely
to apply these days.
 
K

Keith Thompson

<snip>

Aren't identifiers starting with E reserved? Or that's only if you
include <errno.h>?

Only identifiers starting with E followed by either a digit or an
uppercase letter. E and E_TYPE are ok.
 
W

Willem

Walter Roberson wrote:
) The original pointer wanted "the binary form of the variable".

Which you are confusing with "the internal representation of the variable".
"binary" is a counting system, just like "decimal" or "hexadecimal".

If I want the decimal form of the variable, I use printf("%d", ...)
For the hexadecimal form, I use printf("%x", ...)
And for binary, you have to do a bit more work.

<snip>
) The "byte order" is part of the "binary form of the variable",
) at least as I would interpret the phrase "binary form of the variable".

You have a very strange way of interpreting phrases, then.
Like I said, you are confusing it with "internal representation".


SaSW, Willem
--
Disclaimer: I am in no way responsible for any of the statements
made in the above text. For all I know I might be
drugged or something..
No I'm not paranoid. You all think I'm paranoid, don't you !
#EOT
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,431
Messages
2,571,677
Members
48,796
Latest member
Greg L.

Latest Threads

Top