473,385 Members | 2,029 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,385 software developers and data experts.

Getting the xth bit of an unsigned charcter

Hi there,

If I have three unsigned char(8 bits) and I want to compare the xth bit
in each. So if I had.

a 00000000
b 10101010
c 00000000
If I wanted to get the third bit from (a) the 0th bit from (b) and
compare them and store the result in the 7th bit of (c) how could i go
about doing this.

a 000[0]0000
b [1]0101010
c 0000000[0]

0 | 1 = 1

c = 00000001

Would there be a nice function or macro that could do this. Brand new
to all the bitwise stuff.

Nov 14 '05 #1
6 1769
Jo*********@gmail.com wrote:
Hi there, If I have three unsigned char(8 bits) and I want to compare the xth bit
in each. So if I had. a 00000000
b 10101010
c 00000000
If I wanted to get the third bit from (a) the 0th bit from (b) and
compare them and store the result in the 7th bit of (c) how could i go
about doing this. a 000[0]0000
b [1]0101010
c 0000000[0]
The nomenclature I have seen most often calls the lowest order bit
the 0th bit, i.e. just the other way round from what you are calling
them. But YMMV...
0 | 1 = 1 c = 00000001
To get the bits out of a and b in the lowest bit you could use

( a >> 4 | b >> 7 ) & 1

e.g. shifting them first to the lowest position, then ORing both
variables and finally masking out everything but the lowest bit.

And to set the lowest bit of c accordingly (but leaving all other
bits in c intact) you could use

c = ( c & 0xFE ) | ( ( a >> 4 | b >> 7 ) & 1 );

That way you set the lowest bit of c to zero and then OR with the
lowest bit of the result of the shifts, OR and AND on a and b.
Would there be a nice function or macro that could do this. Brand new
to all the bitwise stuff.


Sorry, but I don't know of any ready-made function or macro for such
a rather specialized task. But it wouldn't be too difficult to write
your own function, something like

unsigned char munge_them( unsigned char a, int bit_of_a,
unsigned char b, int bit_of b,
unsigned c )
{
return ( c & 0xFE )
| ( ( ( a >> ( 7 - bit_of_a ) )
| ( b >> ( 7 - bit_of_b ) )
)
& 1
);
}

should do the job (using your nomenclature for naming bit positions).
Admittedly you could leave out some of the parentheses but I guess
it's a bit easier (well, not too much;-) to read that way...

Regards, Jens
--
\ Jens Thoms Toerring ___ Je***********@physik.fu-berlin.de
\__________________________ http://www.toerring.de
Nov 14 '05 #2
Hi.

First you could get the third bit from a and move it into bit 0:
((a & 0x8) >> 3)

Then you could tage bit 0 from b
(b & 0x1)

Then compare them.
(((a & 0x8) >> 3) & (b & 0x1)) << 7

So a macro could be:
#define movesomething(a,b) ((((a & 0x8) >> 3) & (b & 0x1)) << 7)

--
bjrnove

Nov 14 '05 #3
Jo*********@gmail.com wrote:

Hi there,

If I have three unsigned char(8 bits) and I want to compare the xth bit
in each. So if I had.

a 00000000
b 10101010
c 00000000

If I wanted to get the third bit from (a) the 0th bit from (b) and
compare them and store the result in the 7th bit of (c) how could i go
about doing this.
I would call those bits 4, 7, and 0.

a 000[0]0000
b [1]0101010
c 0000000[0]

0 | 1 = 1

c = 00000001

Would there be a nice function or macro that could do this. Brand new
to all the bitwise stuff.


/* BEGIN new.c */

#include <stdio.h>
#include <limits.h>

#define READ_UBIT(U,N) ((U) >> (N) & 1)
#define SET_UBIT(U,N) ((void)((U) |= 1u << (N)))
#define CLEAR_UBIT(U,N) ((void)((U) &= ~(1u << (N))))
#define FLIP_UBIT(U,N) ((void)((U) ^= 1u << (N)))

void bit_str(char *s1, const void *s2, size_t n);

int main(void)
{
unsigned char a, b, c;
char string[CHAR_BIT + 1];

a = 0;
b = 0xaa;
c = 0;
if (READ_UBIT(a, 4) | READ_UBIT(b, 7)) {
SET_UBIT(c, 0);
} else {
CLEAR_UBIT(c, 0);
}
bit_str(string, &a, 1);
printf("a is %s\n", string);
bit_str(string, &b, 1);
printf("b is %s\n", string);
bit_str(string, &c, 1);
printf("c is %s\n", string);
return 0;
}

void bit_str(char *s1, const void *s2, size_t n)
{
unsigned mask;
const unsigned char *const byte = s2;

while (n-- != 0) {
mask = ((unsigned char)-1 >> 1) + 1;
do {
*s1++ = (char)(mask & byte[n] ? '1' : '0');
mask >>= 1;
} while (mask != 0);
}
*s1 = '\0';
}

/* END new.c */

--
pete
Nov 14 '05 #4
Jo*********@gmail.com wrote:

If I have three unsigned char(8 bits) and I want to compare the
xth bit in each. So if I had.

a 00000000
b 10101010
c 00000000

If I wanted to get the third bit from (a) the 0th bit from (b) and
compare them and store the result in the 7th bit of (c) how could
i go about doing this.

a 000[0]0000
b [1]0101010
c 0000000[0]


You could simply write:

putbit(7, c, (getbit(3, a) == getbit(0, b)));

which reduces the problem to writing putbit() and getbit(). After
you solve that you may decide to encapsulate getbit and putbit into
macros, but that is a separate decision. So start with the
frameworks:

void putbit(int bitno, char *byte, int bitvalue) (...}
int getbit(int bitno, char *byte) { ... }

or whatever other similar set tickles your fancy. That will
solidify your understanding of the range and purpose of bitno and
bitvalue. The latter is likely to be most useful if limited to 0
and 1, and similarly for the return values from getbit. With C99
you can use the _Bool type here (or bool after #include
<stdbool.h>). With C90 you would be advised to emulate the C99
techniques with compatible #defines.

--
Chuck F (cb********@yahoo.com) (cb********@worldnet.att.net)
Available for consulting/temporary embedded and systems.
<http://cbfalconer.home.att.net> USE worldnet address!
Nov 14 '05 #5
no*@given.out wrote:

On 11 Mar 2005 02:07:13 -0800, Jo*********@gmail.com wrote:
a 000[0]0000
b [1]0101010
c 0000000[0]


To get the data from a specific bit...

data = varaible & (1 << bit);

A non-0 value in Data indicates the bit was a 1.


Or, to get a result in 0..1 for compatibility with other things,
try:

int getbit(unsigned int bitnum, unsigned int value) {

if (bitnum) value >>= bitnum;
return value & 1;
} /* untested */

--
Chuck F (cb********@yahoo.com) (cb********@worldnet.att.net)
Available for consulting/temporary embedded and systems.
<http://cbfalconer.home.att.net> USE worldnet address!

Nov 14 '05 #6
> CBFalconer <cb********@yahoo.com> wrote:
... to get a result in 0..1 for compatibility with other things,
try:

int getbit(unsigned int bitnum, unsigned int value) {
if (bitnum) value >>= bitnum;
return value & 1;
} /* untested */

Why bother testing bitnum? A shift of 0 is well defined...

int getbit(unsigned x, int n)
{
return (x >> n) & 1;
}

no*@given.out wrote:
int getbit(unsigned int Bitnum, unsigned int Value)
{ return ((Value & (1 << Bitnum)) != 0); }


Better to use 1u rather than 1, because unsigned int will likely
have more value bits than a signed int, so the 1 << Bitnum may
invoke undefined behaviour for a legitimate value of Bitnum.

--
Peter

Nov 14 '05 #7

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

Similar topics

8
by: Timo | last post by:
I am trying to get address of myStruct to a string array texts. I am using M$ Visual C++ 6.0 (this is not OS specific question, though, this code should also work on 16 bit embedded compiler ;)). ...
17
by: Olivier Bellemare | last post by:
I've tried to make a function that returns the middle of a string. For example: strmid("this is a text",6,4); would return "is a". Here is my code: char *strmid(char *texte, int depart,...
5
by: Peter Steele | last post by:
We have an application that when it runs in the IDE in debug mode an unhandled exception is occurring in a system header file associated with STL stirngs. The actual statement that crashes is ...
9
by: Manu | last post by:
Hi, Following code shows how a packet is sent from a client to a server's socket. The file size (fileSizeTemp) is converted to network byte order before sending to the server. *((unsigned...
1
by: Gabi | last post by:
Hi all, i need to find how many times the charcter "\n" is in a string. which string function can give me this information. Thanks an advance
1
by: sangram | last post by:
java script for minimum of 100 charcter in a text area thanks
1
by: girays | last post by:
I have a template class which name is EntityRepository and when I compile this class I get no error. But when I use this class in a main method I get LNK2019 linking error. std::map object is used...
5
by: parag_paul | last post by:
hi All, Even though my function is not extern "C" ed I keep getting this failure ucliForceCmdObject.C", line 194: Warning (Anachronism): Formal argument mhpiConnCb of type extern "C"...
5
by: coleslaw01 | last post by:
Hello, I am trying to teach myself C++ while babysitting a stable network in Iraq and have put together a program to display the fibonacci sequence. It works with long and long double(output in...
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...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
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...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

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.