how to print it out

P

poison.summer

Hi I have a char p,
I'd like to print out the lower 4 bits and higher 4 bits of p
seperately
how can i do that?
Thanks a lot!
 
R

Rod Pemberton

Hi I have a char p,
I'd like to print out the lower 4 bits and higher 4 bits of p
seperately
how can i do that?
Thanks a lot!

I prefer to setup tables, but you don't need to. If you want a simpler
method, the logic that setup_bin_tab uses to setup the string table can be
used without a table. nyb_l is short for nybble_lower, etc. (If you
understand hexadecimal, you can determine the bit patterns in your head.)

Rod Pemberton

//--working--code--

#include <stdio.h>
#include <string.h>

#define nyb_l(a) ((a)&0x0F)
#define nyb_u(a) (((a)>>4)&0x0F)

unsigned char bin_tab[16][5];

void setup_bin_tab(void)
{
unsigned char i,j;

/* set nul's for EOS */
memset(bin_tab,0,sizeof(bin_tab));

for(i=0;i<16;i++)
{
for(j=0;j<4;j++)
{
bin_tab[j]=(i&(1<<(3-j)))?'1':'0';
}
}
}


int main(void)
{
unsigned char p;

setup_bin_tab();

p=0x82;
printf("%02x %s %s\n",p,bin_tab[nyb_u(p)],bin_tab[nyb_l(p)]);

p=0x5A;
printf("%02x %s %s\n",p,bin_tab[nyb_u(p)],bin_tab[nyb_l(p)]);

return(0);
}
 
P

Pedro Graca

Hi I have a char p,
I'd like to print out the lower 4 bits and higher 4 bits of p
seperately
how can i do that?
Thanks a lot!

#include <stdio.h>

int main(void) {
unsigned char p = 42; /* or whatever */
char hibits[5] = {'0', '0', '0', '0', 0};
char lobits[5] = {'0', '0', '0', '0', 0};

if (p > 127) {
hibits[0] = '1';
p -= 128;
}
if (p > 63) {
hibits[1] = '1';
p -= 64;
}
if (p > 31) {
hibits[2] = '1';
p -= 32;
}
if (p > 15) {
hibits[3] = '1';
p -= 16;
}
if (p > 7) {
lobits[0] = '1';
p -= 8;
}
if (p > 3) {
lobits[1] = '1';
p -= 4;
}
if (p > 1) {
lobits[2] = '1';
p -= 2;
}
if (p == 1) {
lobits[3] = '1';
}
printf("Hibits: %s\n", hibits);
printf("Lobits: %s\n", lobits);
return 0;
}
 
J

Joe Wright

Hi I have a char p,
I'd like to print out the lower 4 bits and higher 4 bits of p
seperately
how can i do that?
Thanks a lot!
I give up, how can you? How do you want to print the bits? What problems
do you have when you try it? Talk to me. Show me some C code.
 
S

stathis gotsis

Rod Pemberton said:
Rod Pemberton

//--working--code--

#include <stdio.h>
#include <string.h>

#define nyb_l(a) ((a)&0x0F)
#define nyb_u(a) (((a)>>4)&0x0F)

Is char restricted to 8 bits? If not, maybe this is more general:

#define nyb_u(a) (((a)>>(sizeof(a)*8-4))&0x0F)

unsigned char bin_tab[16][5];

void setup_bin_tab(void)
{
unsigned char i,j;

/* set nul's for EOS */
memset(bin_tab,0,sizeof(bin_tab));

for(i=0;i<16;i++)
{
for(j=0;j<4;j++)
{
bin_tab[j]=(i&(1<<(3-j)))?'1':'0';
}
}
}


int main(void)
{
unsigned char p;

setup_bin_tab();

p=0x82;
printf("%02x %s %s\n",p,bin_tab[nyb_u(p)],bin_tab[nyb_l(p)]);

p=0x5A;
printf("%02x %s %s\n",p,bin_tab[nyb_u(p)],bin_tab[nyb_l(p)]);

return(0);
}
 
J

Jack Klein

Is char restricted to 8 bits? If not, maybe this is more general:

No, char is not restricted to 8 bits, it can't be less buy may be
more. I routinely work on C implementations for DSPs where chars have
#define nyb_u(a) (((a)>>(sizeof(a)*8-4))&0x0F)

Uh, no, while char is not limited to 8 bits, it is limited to being
exactly one byte. sizeof(char) is 1 by definition in C, always has
been, always will be. Even if char contains more than one octet,
which is the proper term for a collection of exactly 8 bits.

So your expression is exactly equivalent to Rod's.

If you want to be truly portable and get the lowest 4 bits and highest
4 bits out of a character, you need to do this:

#define myb_u(a) (((a)>>(CHAR_BIT-4))&0x0f)

....if I counted the parentheses correctly.

On a Texas Instruments TMS320F2812, that would give you bits 15
through 12 of the 16-bit char.
 
K

Keith Thompson

Jack Klein said:
On Tue, 21 Feb 2006 05:00:19 +0200, "stathis gotsis"


No, char is not restricted to 8 bits, it can't be less buy may be
more. I routinely work on C implementations for DSPs where chars have


Uh, no, while char is not limited to 8 bits, it is limited to being
exactly one byte. sizeof(char) is 1 by definition in C, always has
been, always will be. Even if char contains more than one octet,
which is the proper term for a collection of exactly 8 bits.

So your expression is exactly equivalent to Rod's.

Except that character constants are of type int, so nyb_u('x') will
use sizeof('x'), which is the same as sizeof(int). You probably
don't want to apply sizeof to the argument.
 
P

pete

Jack Klein wrote:
If you want to be truly portable and get the lowest 4 bits and highest
4 bits out of a character, you need to do this:

#define myb_u(a) (((a)>>(CHAR_BIT-4))&0x0f)

I don't think that mask does anything.
 
S

stathis gotsis

Jack Klein said:
No, char is not restricted to 8 bits, it can't be less buy may be
more. I routinely work on C implementations for DSPs where chars have


Uh, no, while char is not limited to 8 bits, it is limited to being
exactly one byte. sizeof(char) is 1 by definition in C, always has
been, always will be. Even if char contains more than one octet,
which is the proper term for a collection of exactly 8 bits.

Yes that is correct, terrible confusion. Thank you for the correction. I
should have used CHAR_BIT instead.
 
S

stathis gotsis

Uh, no, while char is not limited to 8 bits, it is limited to being
exactly one byte. sizeof(char) is 1 by definition in C, always has
been, always will be. Even if char contains more than one octet,
which is the proper term for a collection of exactly 8 bits.

I am having second thoughts (or rather questions) about this. I am aware of
implementations which have 16 or 32-bit bytes and sizeof(char)==1. But does
the Standard explicitly state that char must be one byte?
 
P

pete

stathis said:
But does
the Standard explicitly state that char must be one byte?

N869
6.5.3.4 The sizeof operator
[#2] The sizeof operator yields the size (in bytes) of its
operand, which may be an expression or the parenthesized
name of a type. The size is determined from the type of the
operand. The result is an integer. If the type of the
operand is a variable length array type, the operand is
evaluated; otherwise, the operand is not evaluated and the
result is an integer constant.
[#3] When applied to an operand that has type char, unsigned
char, or signed char, (or a qualified version thereof) the
result is 1.
 
K

Keith Thompson

stathis gotsis said:
I am having second thoughts (or rather questions) about this. I am aware of
implementations which have 16 or 32-bit bytes and sizeof(char)==1. But does
the Standard explicitly state that char must be one byte?

Yes.
 
M

Micah Cowan

pete said:
I don't think that mask does anything.

It does if CHAR_BIT > 4, which is why he wrote it using CHAR_BIT in
the first place.

I will note, though, that anyone writing such a thing for a specific
application is probably only intending to be portable to 8-bit-byte
platforms, or perhaps 8-bit data translated to higher-bit-byte
platforms, in which case he or she is probably /actually/ interested
in the first two sets of 4 bits anyway, and not actually the "highest"
and "lowest".

-Micah
 
P

pete

Micah said:
It does if CHAR_BIT > 4, which is why he wrote it using CHAR_BIT in
the first place.

I don't think so.
If CHAR_BIT is 5000, then (a)>>(CHAR_BIT-4)
will be value whose binary representation is 4996 highest order bits
that have zero values,
and 4 lowest order bits which have the same values as the
4 highest order bits of (a).
&0x0f won't change the value of the 4996 zero value high order bits
and it won't change the value of the low order bits.
 
S

stathis gotsis

pete said:
I don't think so.
If CHAR_BIT is 5000, then (a)>>(CHAR_BIT-4)
will be value whose binary representation is 4996 highest order bits
that have zero values,
and 4 lowest order bits which have the same values as the
4 highest order bits of (a).
&0x0f won't change the value of the 4996 zero value high order bits
and it won't change the value of the low order bits.

Yes, that is true. Rod Pemberton used that mask in his not very portable
solution as it was needed then. In the search of a more portable one, we
just forgot to remove it.
 
M

Micah Cowan

pete said:
I don't think so.
If CHAR_BIT is 5000, then (a)>>(CHAR_BIT-4)
will be value whose binary representation is 4996 highest order bits
that have zero values,
and 4 lowest order bits which have the same values as the
4 highest order bits of (a).
&0x0f won't change the value of the 4996 zero value high order bits
and it won't change the value of the low order bits.

No, you're right... I wasn't reading with great care, apparently. :)

-Micah
 
R

Rod Pemberton

stathis gotsis said:
Yes, that is true. Rod Pemberton used that mask in his not very portable
solution as it was needed then. In the search of a more portable one, we
just forgot to remove it.

Mathematically the &0x0F on the upper nybble doesn't do anything. However,
their are certain compilers (i.e., OpenWatcom) which have problems with
implicit casts (i.e., doesn't properly clear upper bits if there is a type
conversion, say 32bits to16bits). The &0x0F forces the compiler to clear
the upper bits of the value.

If you need a more portable method you could this:

#define nyb_n(a,n) (((a)>>(n*4))&0x0F)
#define nyb_0(a) nyb_n(a,0)
#define nyb_1(a) nyb_n(a,1)
#define nyb_2(a) nyb_n(a,2)
#define nyb_3(a) nyb_n(a,3)
etc...

The &0x0F left in for good measure...

Rod Pemberton
 
B

Ben Bacarisse

I don't think so.
If CHAR_BIT is 5000,

I just love this newsgroup!
then (a)>>(CHAR_BIT-4) will be value whose binary
representation is 4996 highest order bits that have zero values,
and 4 lowest order bits which have the same values as the 4 highest order
bits of (a).
&0x0f won't change the value of the 4996 zero value high order bits and it
won't change the value of the low order bits.

but it is, after all, in a macro and might be used to see bits 4999-4996
(the top "character" nibble) of an integer value.
 

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

Forum statistics

Threads
474,175
Messages
2,570,945
Members
47,492
Latest member
gabbywilliam

Latest Threads

Top