How to print out

P

poison.summer

Hi I have a char variable p,
I'd like to print the lower 4 bits and higher 4 bits of p seperately,
how can I do it?

Thanks a lot!
 
P

poison.summer

what if I want it in hex mode?
I did the same thing, but when I use %x, it doesn't work

thanks again
 
K

Keith Thompson

what if I want it in hex mode?

What if you want *what* in hex mode?

Please read said:
I did the same thing, but when I use %x, it doesn't work

What do you mean by "it doesn't work"? Show us some code and tell us
what it does, and how that differs from what you expected it to do,
and we might be able to help.
 
P

Praetorian

what if I want it in hex mode?
I did the same thing, but when I use %x, it doesn't work

thanks again

So the printing as ints worked but hex didn't? I have no idea why this
would happen, you can try cast the result of each masking operation as
an int once again but I doubt this will fix it.
 
K

Keith Thompson

Praetorian said:
So the printing as ints worked but hex didn't? I have no idea why this
would happen, you can try cast the result of each masking operation as
an int once again but I doubt this will fix it.

Sorry, but "try a cast, but I doubt that it will fix it" is bad advice.

All casts should be viewed with suspicion. A cast is often used to
tell the compiler, "I know what I'm doing; don't bother me with
warnings"; if you *don't* know what you're doing, you're very likely
to shoot yourself in the foot. (The classic example is casting the
result of malloc() to disable a warning caused by a missing
"#include <stdlib.h".)

We need to see the OP's actual code before we can offer any advice;
trying to guess what "it doesn't work" really means won't do any good.
(If the OP doesn't come back and tell us what he meant, we can assume
he didn't really need or want any help.)
 
M

Micah Cowan

Keith Thompson said:
Sorry, but "try a cast, but I doubt that it will fix it" is bad advice.

All casts should be viewed with suspicion. A cast is often used to
tell the compiler, "I know what I'm doing; don't bother me with
warnings"; if you *don't* know what you're doing, you're very likely
to shoot yourself in the foot. (The classic example is casting the
result of malloc() to disable a warning caused by a missing
"#include <stdlib.h".)

We need to see the OP's actual code before we can offer any advice;
trying to guess what "it doesn't work" really means won't do any good.
(If the OP doesn't come back and tell us what he meant, we can assume
he didn't really need or want any help.)

We're missing some context. The message to which poison.summer was
responding, written by Praetorian, had:
printf("Lower = %d, Upper = %d",(int)p&0xF,((int)p&0xF0)>>4);

In this case, if %d is changed to %x, casting (or rather, /changing/
the existing casts to (unsigned int)) seems appropriate (if perhaps
unnecessary, depending on the type of p).

However, telling us "it doesn't work" is not informative. Please post
a smallish amount of code that will compile (has a main() function,
etc.) that illustrates the problem, along with the output you are
getting from it.

-Micah
 
J

Jack Klein

Hi I have a char variable p,
I'd like to print the lower 4 bits and higher 4 bits of p seperately,
how can I do it?

Thanks a lot!

Which four of the 32 bits in the char are "lower" and which are
"higher"? If you had said "lowest" and "highest", we would know for
sure what you were talking about. And what do you want to do with the
other 24 bits?

And why are you posting this again instead of reading the replies to
your previous post of the same question?
 
P

Praetorian

Sorry, but "try a cast, but I doubt that it will fix it" is bad advice.

All casts should be viewed with suspicion. A cast is often used to
tell the compiler, "I know what I'm doing; don't bother me with
warnings"; if you *don't* know what you're doing, you're very likely
to shoot yourself in the foot. (The classic example is casting the
result of malloc() to disable a warning caused by a missing
"#include <stdlib.h".)

We need to see the OP's actual code before we can offer any advice;
trying to guess what "it doesn't work" really means won't do any good.
(If the OP doesn't come back and tell us what he meant, we can assume
he didn't really need or want any help.)

I agree that typecasting when you don't know what you're doing can get
you into trouble but all I asked OP to do was cast the result of the
bitwise operations in the following line of code to an int once again.

printf("Lower = %d, Upper = %d",(int)p&0xF,((int)p&0xF0)>>4);

I don't think typecasting can hurt in anyway in this particular case.
 
P

Praetorian

Jack said:
Which four of the 32 bits in the char are "lower" and which are
"higher"? If you had said "lowest" and "highest", we would know for
sure what you were talking about. And what do you want to do with the
other 24 bits?

And why are you posting this again instead of reading the replies to
your previous post of the same question?

A 32-bit char?
 
P

pete

A 32-bit char?

The number of bits in a byte, is equal to CHAR_BIT,
which is defined in <limits.h>.
CHAR_BIT is always at least 8.
There is no maximum limit specified for the value of CHAR_BIT.
 

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,175
Messages
2,570,944
Members
47,491
Latest member
mohitk

Latest Threads

Top