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

hi,how to define a bitstring type in c/c++?

wwj
hello,everyone.I am a new one here.I want to know how to define a
bitstring type in c/c++? and can specify its length.Then I can use it
like:
bitstring a[6]='100111' ;or so.
thank you very much
Nov 13 '05 #1
2 6820
wwj wrote:
hello,everyone.I am a new one here.I want to know how to define a
bitstring type in c/c++? and can specify its length.Then I can use it
like:
bitstring a[6]='100111' ;or so.


This newsgroup is for C questions. For C++ questions, please ask in
comp.lang.c++ (where the answer is very different).

The typical C solution is to define an array of unsigned char, and use some
macros to do the CHAR_BIT multiply for you.

See if you can work out how these work. If you can't, see if you can just
work out how to use them (a rather easier task).

#include <limits.h>

#define SET_BIT(a, n) (a)[(n) / CHAR_BIT] |= \
(unsigned char)(1U << ((n) % CHAR_BIT))
#define CLEAR_BIT(a, n) (a)[(n) / CHAR_BIT] &= \
(unsigned char)(~(1U << ((n) % CHAR_BIT)))
#define TEST_BIT(a, n) (((a)[(n) / CHAR_BIT] & \
(unsigned char)(1U << ((n) % CHAR_BIT))) ? 1 : 0)


--
Richard Heathfield : bi****@eton.powernet.co.uk
"Usenet is a strange place." - Dennis M Ritchie, 29 July 1999.
C FAQ: http://www.eskimo.com/~scs/C-faq/top.html
K&R answers, C books, etc: http://users.powernet.co.uk/eton
Nov 13 '05 #2
wwj
Thank you very much!

Richard Heathfield <do******@address.co.uk.invalid> wrote in message news:<bn**********@sparta.btinternet.com>...
wwj wrote:
hello,everyone.I am a new one here.I want to know how to define a
bitstring type in c/c++? and can specify its length.Then I can use it
like:
bitstring a[6]='100111' ;or so.


This newsgroup is for C questions. For C++ questions, please ask in
comp.lang.c++ (where the answer is very different).

The typical C solution is to define an array of unsigned char, and use some
macros to do the CHAR_BIT multiply for you.

See if you can work out how these work. If you can't, see if you can just
work out how to use them (a rather easier task).

#include <limits.h>

#define SET_BIT(a, n) (a)[(n) / CHAR_BIT] |= \
(unsigned char)(1U << ((n) % CHAR_BIT))
#define CLEAR_BIT(a, n) (a)[(n) / CHAR_BIT] &= \
(unsigned char)(~(1U << ((n) % CHAR_BIT)))
#define TEST_BIT(a, n) (((a)[(n) / CHAR_BIT] & \
(unsigned char)(1U << ((n) % CHAR_BIT))) ? 1 : 0)

Nov 13 '05 #3

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

Similar topics

3
by: theotyflos | last post by:
Hi all, I have the following: /*--- SNIP ---*/ typedef struct Argument_s { char *address; int type;
7
by: No Spam | last post by:
----snip #define POSITIVE_INTEGRATOR_SATURATION 0x03000000L // #define NEGATIVE_INTEGRATOR_SATURATION 0xFD000000L // long integrator; integrator=0;
42
by: baumann | last post by:
hi all, typedef int (*pfunc)(int , int); pfunc a_func; i know it's ok, but how can define a_func without typedef statement? thanks .
10
by: O Plameras | last post by:
Are there differences in terms of functionality of, #define and typedef ? By the above I mean any instance when the outcome obtained by running two versions (1) and (2) below of C codes...
15
by: Ben Hinkle | last post by:
I'm curious, what was the rationale for making a builtin type _Bool but then having #define true 1 #define false 0 in stdbool.h? That seems very odd that true and false don't have type _Bool. In...
12
by: Just D | last post by:
All, It was possible before in Pascal, C++, etc. to define our custom data type or redefine the existing type, like in Turbo Pascal we could assume that shortint is int and use all references to...
71
by: David T. Ashley | last post by:
Where is the best place to define TRUE and FALSE? Are they in any of the standard include files, ever? Do any standards apply? What I've traditionally done is something like: #ifndef...
10
by: Yevgen Muntyan | last post by:
Consider the following macro: #define ALLOCIT(Type) ((Type*) malloc (sizeof (Type))) The intent is to wrap raw memory allocation of N bytes into a macro which returns allocated chunk of memory...
10
by: Russell Mangel | last post by:
Hi, This programs converts a BitString "0101110" to an Int32 value. Can anyone improve, or possibly shorten the following program? public static int BitStringToInt32(String s) { int j, k, m...
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...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
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
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
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.