473,387 Members | 1,790 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

How to print out

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!

Feb 20 '06 #1
10 1443

poison.sum...@gmail.com wrote:
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!


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

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

thanks again

Feb 20 '06 #3
po***********@gmail.com writes:
what if I want it in hex mode?
What if you want *what* in hex mode?

Please read <http://cfaj.freeshell.org/google/>.
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.

--
Keith Thompson (The_Other_Keith) ks***@mib.org <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst>
We must do something. This is something. Therefore, we must do this.
Feb 20 '06 #4

po***********@gmail.com wrote:
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.

Feb 20 '06 #5
"Praetorian" <as***************@gmail.com> writes:
po***********@gmail.com wrote:
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.


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.)

--
Keith Thompson (The_Other_Keith) ks***@mib.org <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst>
We must do something. This is something. Therefore, we must do this.
Feb 21 '06 #6
Keith Thompson <ks***@mib.org> writes:
"Praetorian" <as***************@gmail.com> writes:
po***********@gmail.com wrote:
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.


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
Feb 21 '06 #7
On 20 Feb 2006 13:02:30 -0800, po***********@gmail.com wrote in
comp.lang.c:
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?

--
Jack Klein
Home: http://JK-Technology.Com
FAQs for
comp.lang.c http://c-faq.com/
comp.lang.c++ http://www.parashift.com/c++-faq-lite/
alt.comp.lang.learn.c-c++
http://www.contrib.andrew.cmu.edu/~a...FAQ-acllc.html
Feb 21 '06 #8
>
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.)

--
Keith Thompson (The_Other_Keith) ks***@mib.org <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst>
We must do something. This is something. Therefore, we must do this.


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.

Feb 21 '06 #9

Jack Klein wrote:
On 20 Feb 2006 13:02:30 -0800, po***********@gmail.com wrote in
comp.lang.c:
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?

--
Jack Klein
Home: http://JK-Technology.Com
FAQs for
comp.lang.c http://c-faq.com/
comp.lang.c++ http://www.parashift.com/c++-faq-lite/
alt.comp.lang.learn.c-c++
http://www.contrib.andrew.cmu.edu/~a...FAQ-acllc.html


A 32-bit char?

Feb 21 '06 #10
Praetorian wrote:
Which four of the 32 bits in the char are "lower" and which are
"higher"?
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.

--
pete
Feb 21 '06 #11

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

12
by: Michael Foord | last post by:
Here's a little oddity with 'print' being a reserved word... >>> class thing: pass >>> something = thing() >>> something.print = 3 SyntaxError: invalid syntax >>> print something.__dict__...
14
by: Marcin Ciura | last post by:
Here is a pre-PEP about print that I wrote recently. Please let me know what is the community's opinion on it. Cheers, Marcin PEP: XXX Title: Print Without Intervening Space Version:...
0
by: bearophileHUGS | last post by:
There is/was a long discussion about the replacement for print in Python 3.0 (I don't know if this discussion is finished): http://mail.python.org/pipermail/python-dev/2005-September/055968.html ...
1
by: hamil | last post by:
I am trying to print a graphic file (tif) and also use the PrintPreview control, the PageSetup control, and the Print dialog control. The code attached is a concatination of two examples taken out...
1
by: Steff | last post by:
I am wandering if my code is making sense... I use a lot the print function. Is it weird in this case where I have to display an array ? I thought it would be better to have the entire array in php...
3
by: James J. Besemer | last post by:
I would like to champion a proposed enhancement to Python. I describe the basic idea below, in order to gage community interest. Right now, it's only an idea, and I'm sure there's room for...
69
by: Edward K Ream | last post by:
The pros and cons of making 'print' a function in Python 3.x are well discussed at: http://mail.python.org/pipermail/python-dev/2005-September/056154.html Alas, it appears that the effect of...
2
by: Brad Pears | last post by:
I have some sample code that uses the print dialog, print preview and a print direct options. If I select print preview and then click the printer icon from that, the document prints. If I...
7
by: samslists | last post by:
Am I the only one that thinks this would be useful? :) I'd really like to be able to use python 3.0's print statement in 2.x. Is this at least being considered as an option for 2.6? It seems...
12
by: Studiotyphoon | last post by:
Hi, I have report which I need to print 3 times, but would like to have the following headings Customer Copy - Print 1 Accounts Copy - Print 2 File Copy -Print 3 I created a macro to...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.