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! 17 1497
<po***********@ gmail.com> wrote in message
news:11******** **************@ g14g2000cwa.goo glegroups.com.. . 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(v oid)
{
unsigned char i,j;
/* set nul's for EOS */
memset(bin_tab, 0,sizeof(bin_ta b));
for(i=0;i<16;i+ +)
{
for(j=0;j<4;j++ )
{
bin_tab[i][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);
} po***********@g mail.com wrote: 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;
}
--
If you're posting through Google read <http://cfaj.freeshell. org/google> po***********@g mail.com wrote: 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.
--
Joe Wright
"Everything should be made as simple as possible, but not simpler."
--- Albert Einstein ---
"Rod Pemberton" <do*********@so rry.bitbucket.c mm> wrote in message
news:dt******** **@news1.greatn owhere.com... <po***********@ gmail.com> wrote in message news:11******** **************@ g14g2000cwa.goo glegroups.com.. . Hi I have a char p, I'd like to print out the lower 4 bits and higher 4 bits of p seperately 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(v oid) { unsigned char i,j;
/* set nul's for EOS */ memset(bin_tab, 0,sizeof(bin_ta b));
for(i=0;i<16;i+ +) { for(j=0;j<4;j++ ) { bin_tab[i][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); }
On Tue, 21 Feb 2006 05:00:19 +0200, "stathis gotsis"
<st***********@ hotmail.com> wrote in comp.lang.c: "Rod Pemberton" <do*********@so rry.bitbucket.c mm> wrote in message news:dt******** **@news1.greatn owhere.com... <po***********@ gmail.com> wrote in message news:11******** **************@ g14g2000cwa.goo glegroups.com.. . Hi I have a char p, I'd like to print out the lower 4 bits and higher 4 bits of p seperately 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:
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
16 or 32 bits. The macro CHAR_BIT in <limits.h> gives the number of
bits.
#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_BI T-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.
--
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.l earn.c-c++ http://www.contrib.andrew.cmu.edu/~a...FAQ-acllc.html
Jack Klein <ja*******@spam cop.net> writes: On Tue, 21 Feb 2006 05:00:19 +0200, "stathis gotsis" <st***********@ hotmail.com> wrote in comp.lang.c:
[...] 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 16 or 32 bits. The macro CHAR_BIT in <limits.h> gives the number of bits.
#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.
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.
--
Keith Thompson (The_Other_Keit h) 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.
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_BI T-4))&0x0f)
I don't think that mask does anything.
--
pete
"Jack Klein" <ja*******@spam cop.net> wrote in message
news:39******** *************** *********@4ax.c om... On Tue, 21 Feb 2006 05:00:19 +0200, "stathis gotsis" <st***********@ hotmail.com> wrote in comp.lang.c:
"Rod Pemberton" <do*********@so rry.bitbucket.c mm> wrote in message news:dt******** **@news1.greatn owhere.com... <po***********@ gmail.com> wrote in message news:11******** **************@ g14g2000cwa.goo glegroups.com.. . > Hi I have a char p, > I'd like to print out the lower 4 bits and higher 4 bits of p > seperately 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:
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 16 or 32 bits. The macro CHAR_BIT in <limits.h> gives the number of bits.
#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.
Yes that is correct, terrible confusion. Thank you for the correction. I
should have used CHAR_BIT instead.
"Jack Klein" <ja*******@spam cop.net> wrote in message
news:39******** *************** *********@4ax.c om... 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? This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics |
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__
{}
|
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: $Revision: 0.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
There is also a wiki page that collects the ideas:
http://wiki.python.org/moin/PrintAsFunction
There is the will to keep the printing operation very simple for the
most common situation, but at the same time to make it flexible
(optional no...
|
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 of a Microsoft book,
"Visual Basic,Net Step by Step" in Chapter 18.
All but the bottom two subroutines will open a text file, and then allow me
to use the above controls, example 1. The bottom two subroutines will print
a graphic file, example...
|
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 but now I am not sure if
that makes sense. Can you tell me please ?
<html>
<head>
| |
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 improvement. And of course it's
possible there's some serious "gotcha" I've overlooked. Thus I welcome any
and all comments.
If there's some agreement that this proposal is worth further consideration
then I'll re-submit a formal document in...
|
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 this pep would be to make it impossible
to use the name 'print' in a backward compatible manner. Indeed, if a
program is to compile in both Python 2.x and Python 3.x, the print function
(or the print statement with parentheses) can not use the...
|
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 select the print directly option, it also prints right
away to the defauilt printer.
However, if I use the printer dialog control to print and I click 'OK' to
actually print the document - nothing happens. The job does not even go into
the print...
|
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 like it would be helpful with transitioning.
|
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 print the report three times, but do not know how
|
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look !
Part I. Meaning of...
| |
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it.
First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
|
by: Oralloy |
last post by:
Hello folks,
I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>".
The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed.
This is as boiled down as I can make it.
Here is my compilation command:
g++-12 -std=c++20 -Wnarrowing bit_field.cpp
Here is the code in...
|
by: Hystou |
last post by:
Overview:
Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
|
by: tracyyun |
last post by:
Dear forum friends,
With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
|
by: agi2029 |
last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own....
Now, this would greatly impact the work of software developers. The idea...
|
by: adsilva |
last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
| |
by: muto222 |
last post by:
How can i add a mobile payment intergratation into php mysql website.
|
by: bsmnconsultancy |
last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...
| |