473,396 Members | 1,754 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,396 software developers and data experts.

Macro for counting the bits in odd positions

I need a macro for counting the bits in the odd positions of a given
input (of any type, char, pointer, int, struct, whatever).
Is there any clever way I could not think of, to do it efficiently?

#define COUNT(num, count) \

do { \

unsigned char *safe ## num ## count = (unsigned
char *)(&(num)); \

size_t safe ## size = sizeof(num); \

count=0; \

while (safe ## size--) { \

count+=(((*safe ## num ## count &
0x2)!=0)+((*safe ## num ## count & 0x8)!=0)+((*safe ## num ## count &
0x20)!=0)+((*safe ## num ## count & 0x80)!=0)); \

safe ## num ## count++; \

} \

} while (0)

Thanks

Nov 15 '05 #1
7 2836
In article <11**********************@o13g2000cwo.googlegroups .com>,
zets <zo***********@gmail.com> wrote:
I need a macro for counting the bits in the odd positions of a given
input (of any type, char, pointer, int, struct, whatever).
Is there any clever way I could not think of, to do it efficiently?


How do you deal with the padding in structures?

Is it meaningful to look at the bit level of pointers? Pointers
are somewhat defined as opaque tokens, and casting a pointer
is not certain to give you the same bitwise representation even if
the same location in memory is being represented.

Does your application take into account 1's complement, separate
sign bits, differing representations for floats, and all the other ways
that numbers might be stored differently in different versions of C?
For example when CHAR_BIT is odd, then "odd" and "even" bits
is contextual and dependant on byte ordering. Your code does not
take that into account: it resets the notion of odd vs even on
each character boundary.

--
"Who Leads?" / "The men who must... driven men, compelled men."
"Freak men."
"You're all freaks, sir. But you always have been freaks.
Life is a freak. That's its hope and glory." -- Alfred Bester, TSMD
Nov 15 '05 #2


zets wrote:
I need a macro for counting the bits in the odd positions of a given
input (of any type, char, pointer, int, struct, whatever).


Out of curiosity, why do you need such a thing? That's
two questions, really: Why do you need this silly-seeming
operation, and why do you need it in the form of a macro?

--
Er*********@sun.com

Nov 15 '05 #3
A student of mine sent me a question given in an exam. the question was
to write this macro, it is not useful for any real application. This
was my solution, just wanted to check out if there's any trick I was
missing here.

Nov 15 '05 #4
In article <11**********************@o13g2000cwo.googlegroups .com>,
zets <zo***********@gmail.com> wrote:
I need a macro for counting the bits in the odd positions of a given
input (of any type, char, pointer, int, struct, whatever).
Is there any clever way I could not think of, to do it efficiently?

#define COUNT(num, count) \

do { \

unsigned char *safe ## num ## count = (unsigned
char *)(&(num)); \


If num is a numeric constant (including a character literal) then
you will not be able to take its address.

If num is a string constant (double quoted string) then the macro
would attempt to count over the pointer to the string -- but the
pointer to a string constant is not (if I recall properly) certain
to be the same for all copies of the string, so examining the bits
is not necessarily going to give you a consistant answer.
--
'The short version of what Walter said is "You have asked a question
which has no useful answer, please reconsider the nature of the
problem you wish to solve".' -- Tony Mantler
Nov 15 '05 #5
On Tue, 09 Aug 2005 11:03:37 -0700, zets wrote:
I need a macro for counting the bits in the odd positions of a given
input (of any type, char, pointer, int, struct, whatever).
Is there any clever way I could not think of, to do it efficiently?
I don't know of a better way than two loops, without having some
hardware-specific knowledge/help.
#define COUNT(num, count) \

do { \

unsigned char *safe ## num ## count = (unsigned
char *)(&(num)); \

size_t safe ## size = sizeof(num); \

count=0; \

while (safe ## size--) { \

count+=(((*safe ## num ## count &
0x2)!=0)+((*safe ## num ## count & 0x8)!=0)+((*safe ## num ## count &
0x20)!=0)+((*safe ## num ## count & 0x80)!=0)); \

safe ## num ## count++; \

} \

} while (0)


It's more intuitive to me to count the bits from the leftmost bit. You
are counting from the rightmost bit.

You are also missing the inner loop because your code assumes that
CHAR_BIT == 8. This modification of your macro will work for any value of
CHAR_BIT - odd or even - assuming that bits are counted from the leftmost
bit of the object represented by "num".

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

#define COUNT(num, count) \
do { \
unsigned char *safe##num##count = (unsigned char *)(&(num)); \
size_t safe##size = sizeof(num); \
unsigned char bit = 1; \
count = 0; \
while (safe##size--) { \
bit <<= (CHAR_BIT - 2); \
do { \
count += (*safe##num##count & bit) > 0; \
if (bit <= 2) \
break; \
bit >>= 2; \
} while (1); \
safe##num##count++; \
} \
} while (0)

--
http://members.dodo.com.au/~netocrat

Nov 15 '05 #6
"zeus" <zo***@sheernetworks.com> writes:
A student of mine sent me a question given in an exam. the question was
to write this macro, it is not useful for any real application. This
was my solution, just wanted to check out if there's any trick I was
missing here.


Please don't assume that your readers have easy access to the article
to which you're replying. You need to provide some context, including
appropriate quoted text from the previous article and proper
attributions.

Google makes this needlessly difficult, but not impossible. If you
had been following this newsgroup, you would have seen the following
many many times:

If you want to post a followup via groups.google.com, don't use
the broken "Reply" link at the bottom of the article. Click on
"show options" at the top of the article, then click on the
"Reply" at the bottom of the article headers.

--
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.
Nov 15 '05 #7
On Wed, 10 Aug 2005 08:26:30 +1000, Netocrat wrote:
On Tue, 09 Aug 2005 11:03:37 -0700, zets wrote:
I need a macro for counting the bits in the odd positions of a given
input (of any type, char, pointer, int, struct, whatever).
<snip>
count+=(((*safe ## num ## count &
0x2)!=0)+((*safe ## num ## count & 0x8)!=0)+((*safe ## num ## count &
0x20)!=0)+((*safe ## num ## count & 0x80)!=0)); \
<snip> It's more intuitive to me to count the bits from the leftmost bit. You
are counting from the rightmost bit.
My mistake - you are counting from the leftmost bit after all.
You are also missing the inner loop because your code assumes that
CHAR_BIT == 8. This modification of your macro will work for any value of
CHAR_BIT - odd or even - assuming that bits are counted from the leftmost
bit of the object represented by "num".
And I am counting even bits not odd bits... the initialisation of bit
should be 2 not 1.
#include <stdio.h>
#include <limits.h>

#define COUNT(num, count) \
do { \
unsigned char *safe##num##count = (unsigned char *)(&(num)); \
size_t safe##size = sizeof(num); \
unsigned char bit = 1; \
count = 0; \
while (safe##size--) { \
bit <<= (CHAR_BIT - 2); \
do { \
count += (*safe##num##count & bit) > 0; \
if (bit <= 2) \
break; \
bit >>= 2; \
} while (1); \
safe##num##count++; \
} \
} while (0)


--
http://members.dodo.com.au/~netocrat

Nov 15 '05 #8

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

Similar topics

10
by: Eric | last post by:
I have an array that contains over 30000+ bits. The size varies at runtime. I need to move through this chunk of memory and count bits every so often. Example: First 9 bits has 2 ones, next 10...
26
by: G Patel | last post by:
Hi, I'm wondering if anyone knows if the following function will function properly as a set-bit counter on non 2s complement machines (as K&R2 implies). | int bitcount(unsigned x) | { | ...
7
by: sathyashrayan | last post by:
Group, Following function will check weather a bit is set in the given variouble x. int bit_count(long x) { int n = 0; /* ** The loop will execute once for each bit of x set,
14
by: Malcolm | last post by:
Hi, I have the following which fails with "disagreement in number of macro arguments" when compiling with Imagecraft ICCAVR. Has anyone got any ideas - its not vital but would make the code a...
3
by: 4600cc | last post by:
Hi. This might sound dumb, but I need some guidence. I need to know how to set bits to 1 and to 0, on and off. Thus far I can set a single bit to 1 by using LSH and bitwise OR operators, but...
13
by: Tomás | last post by:
The quantity of bits used by an unsigned integer type in memory can be determined by: typedef unsigned long long UIntType; CHAR_BIT * sizeof(UIntType) However, what would be a good...
5
by: Oyvind Eriksen | last post by:
Hello. I need to read bits from bytes in a file. I have code that works but it's very slow. Can anybody help me? The code I have is: private bool GetBit(byte b, int pos) { return ((b &...
34
by: Cuthbert | last post by:
Hi folks, I am trying to find a more efficient way to count "How many bits are '1' in a integer variable?". I still have no idea to count the bits except using a loop and "if" statements....
8
by: te509 | last post by:
Hi guys, does anybody know how to convert a long sequence of bits to a bit-string? I want to avoid this: '949456129574336313917039111707606368434510426593532217946399871489' I would...
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: 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...
0
tracyyun
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...
0
agi2029
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,...

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.