473,659 Members | 2,659 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Having trouble understanding these bitwise macros

Hello!

First things first (but not necessarily in that order), this is a
really great group, and has helped me understand more and more C
everytime I read the postings, so thanks for a great learning
environment!

I am having difficulty understanding this bitwise macro (i've recently
moved into working on these, using OpenSolaris source code -
magnificent learning resource btw!)

#define setbit(a, i) ((a)[(i)/NBBY] |= 1<<((i)%NBBY))
#define clrbit(a, i) ((a)[(i)/NBBY] &= ~(1<<((i)%NBBY) ))
#define isset(a, i) ((a)[(i)/NBBY] & (1<<((i)%NBBY)) )

where NBBY is defined as 8

I imagine that "a" is a 32bit int, and i is the position of the bit in
the word/int that wants to be set. I don't understand why they divide
by NBBY though? I sort of understand the rest, but if someone could
perhaps spare the time to explain the "setbit" macro, i can extrapolate
the rest for myself.

Many thanks!!

KB

Nov 15 '05 #1
5 2506
Krumble Bunk wrote:
Hello!

First things first (but not necessarily in that order), this is a
really great group, and has helped me understand more and more C
everytime I read the postings, so thanks for a great learning
environment!

I am having difficulty understanding this bitwise macro (i've recently
moved into working on these, using OpenSolaris source code -
magnificent learning resource btw!)

#define setbit(a, i) ((a)[(i)/NBBY] |= 1<<((i)%NBBY))
#define clrbit(a, i) ((a)[(i)/NBBY] &= ~(1<<((i)%NBBY) ))
#define isset(a, i) ((a)[(i)/NBBY] & (1<<((i)%NBBY)) )

where NBBY is defined as 8

I imagine that "a" is a 32bit int, and i is the position of the bit in
the word/int that wants to be set. I don't understand why they divide
by NBBY though? I sort of understand the rest, but if someone could
perhaps spare the time to explain the "setbit" macro, i can extrapolate
the rest for myself.

No, 'a' is not an int. It can't be, because the indexing operator []
couldn't be applied.

These macros are intended to maintain a bit array, where it is assumed
that there are at least NBBY (Number of Bits per BYte, I assume) bits to
every element of a (that is, 'a' will probably be of type 'unsigned
char[]', for some size).

They say a picture's worth a thousand words. Imagine 'a' like this

a[0] a[1] a[2]
01234567 01234567 01234567
01010011 01000101 01011000 ...

Here setbit(a, 14) expands to ((a)[14/8] |= (1 << (14%8)), hence a[1] |=
1 << 6, setting bit 6 of a[1], which is the 15th bit in the collection.

S.
Nov 15 '05 #2

"Krumble Bunk" <kr*********@gm ail.com> wrote in message
news:11******** **************@ g14g2000cwa.goo glegroups.com.. .
Hello!

First things first (but not necessarily in that order), this is a
really great group, and has helped me understand more and more C
everytime I read the postings, so thanks for a great learning
environment!

I am having difficulty understanding this bitwise macro (i've recently
moved into working on these, using OpenSolaris source code -
magnificent learning resource btw!)

#define setbit(a, i) ((a)[(i)/NBBY] |= 1<<((i)%NBBY))
#define clrbit(a, i) ((a)[(i)/NBBY] &= ~(1<<((i)%NBBY) ))
#define isset(a, i) ((a)[(i)/NBBY] & (1<<((i)%NBBY)) )

where NBBY is defined as 8

I imagine that "a" is a 32bit int, and i is the position of the bit in
the word/int that wants to be set. I don't understand why they divide
by NBBY though? I sort of understand the rest, but if someone could
perhaps spare the time to explain the "setbit" macro, i can extrapolate
the rest for myself.


Seems to set/test a variable's bits through a pointer ...

#include <stdio.h>

#define NBBY 8

#define setbit(a, i) ((a)[(i)/NBBY] |= 1<<((i)%NBBY))
#define clrbit(a, i) ((a)[(i)/NBBY] &= ~(1<<((i)%NBBY) ))
#define isset(a, i) ((a)[(i)/NBBY] & (1<<((i)%NBBY)) )

int main(void)
{
int b = 0;

int * a = &b;

puts("Before setbit");

printf("a's address = %p b's value = %d\n", a, b);

setbit(a, 2);

puts("After setbit");

printf("a's address = %p b's value = %d isset = %d\n", a, b,
!!isset(a, 2));

return 0;
}

Before setbit
a's address = 0012FF78 b's value = 0
After setbit
a's address = 0012FF78 b's value = 4 isset = 1

Code above translates to ...

int main(void)
{
int b = 0;

int * a = &b;

puts("Before setbit");

printf("a's address = %p b's value = %d\n", a, b);

((a)[(2)/8] |= 1<<((2)%8));

puts("After setbit");

printf("a's address = %p b's value = %d isset = %d\n", a, b,
!!((a)[(2)/8] & (1<<((2)%8))));

return 0;
}
Nov 15 '05 #3
pemo wrote:
"Krumble Bunk" <kr*********@gm ail.com> wrote in message
news:11******** **************@ g14g2000cwa.goo glegroups.com.. .
Hello!

First things first (but not necessarily in that order), this is a
really great group, and has helped me understand more and more C
everytime I read the postings, so thanks for a great learning
environment!

I am having difficulty understanding this bitwise macro (i've recently
moved into working on these, using OpenSolaris source code -
magnificent learning resource btw!)

#define setbit(a, i) ((a)[(i)/NBBY] |= 1<<((i)%NBBY))
#define clrbit(a, i) ((a)[(i)/NBBY] &= ~(1<<((i)%NBBY) ))
#define isset(a, i) ((a)[(i)/NBBY] & (1<<((i)%NBBY)) )

where NBBY is defined as 8

I imagine that "a" is a 32bit int, and i is the position of the bit in
the word/int that wants to be set. I don't understand why they divide
by NBBY though? I sort of understand the rest, but if someone could
perhaps spare the time to explain the "setbit" macro, i can extrapolate
the rest for myself.


Seems to set/test a variable's bits through a pointer ...

#include <stdio.h>

#define NBBY 8

#define setbit(a, i) ((a)[(i)/NBBY] |= 1<<((i)%NBBY))
#define clrbit(a, i) ((a)[(i)/NBBY] &= ~(1<<((i)%NBBY) ))
#define isset(a, i) ((a)[(i)/NBBY] & (1<<((i)%NBBY)) )

int main(void)
{
int b = 0;

int * a = &b;

puts("Before setbit");

printf("a's address = %p b's value = %d\n", a, b);

setbit(a, 2);

puts("After setbit");

printf("a's address = %p b's value = %d isset = %d\n", a, b,
!!isset(a, 2));

return 0;
}

Before setbit
a's address = 0012FF78 b's value = 0
After setbit
a's address = 0012FF78 b's value = 4 isset = 1

Code above translates to ...

int main(void)
{
int b = 0;

int * a = &b;

puts("Before setbit");

printf("a's address = %p b's value = %d\n", a, b);

((a)[(2)/8] |= 1<<((2)%8));

puts("After setbit");

printf("a's address = %p b's value = %d isset = %d\n", a, b,
!!((a)[(2)/8] & (1<<((2)%8))));

return 0;
}


Thanks to both of you for your superb replies - exactly what I was
after!

Thanks again -- kb

Nov 15 '05 #4
pemo wrote:
#include <stdio.h>

#define NBBY 8
There is a standard macro CHAR_BIT which tells the number of
bits per byte.

#define setbit(a, i) ((a)[(i)/NBBY] |= 1<<((i)%NBBY))
#define clrbit(a, i) ((a)[(i)/NBBY] &= ~(1<<((i)%NBBY) ))
#define isset(a, i) ((a)[(i)/NBBY] & (1<<((i)%NBBY)) )

int main(void)
{
int b = 0;
int * a = &b;


Should be: unsigned char *a = (unsigned char *)&b;

Your example code would have failed if you use any index
greater than 7.

Nov 15 '05 #5
Old Wolf wrote:
pemo wrote:
#include <stdio.h>

#define NBBY 8

There is a standard macro CHAR_BIT which tells the number of
bits per byte.

Depending on the problem at hand, it may or may not be correct to use
CHAR_BIT rather than 8. For a generic bit array where the goal is to
squeeze as many bits as possible in those bytes, CHAR_BIT is
appropriate, but there may be a need for bit arrays that always use
exactly 8 bits per char, even if it can store more.
#define setbit(a, i) ((a)[(i)/NBBY] |= 1<<((i)%NBBY))
#define clrbit(a, i) ((a)[(i)/NBBY] &= ~(1<<((i)%NBBY) ))
#define isset(a, i) ((a)[(i)/NBBY] & (1<<((i)%NBBY)) )

int main(void)
{
int b = 0;
int * a = &b;

Should be: unsigned char *a = (unsigned char *)&b;

Not a good idea. Declaring b as an unsigned char[] of an appropriate
size in the first place would be better. The code as originally posted
isn't wrong, though.
Your example code would have failed if you use any index
greater than 7.

Obviously, and your revised version could fail for any index greater
than 15. That's assuming that writing to parts of an int through an
unsigned char* is defined in the first place, of course, which I don't know.

S.
Nov 15 '05 #6

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

Similar topics

9
4950
by: Penn Markham | last post by:
Hello all, I am writing a script where I need to use the system() function to call htpasswd. I can do this just fine on the command line...works great (see attached file, test.php). When my webserver runs that part of the script (see attached file, snippet.php), though, it doesn't go through. I don't get an error message or anything...it just returns a "1" (whereas it should return a "0") as far as I can tell. I have read the PHP...
7
3308
by: | last post by:
I fail to understand why that the memory allocated in the void create(int **matrix) does not remain. I passed the address of matrix so shouldn't it still have the allocated memory when it returns to main. The problem i am having is understanding why the printf statement in the code below gives the value. I would have expected it to be 123 which is the value I set it to in the create. Thanx in advance. void create(int **matrix); int...
9
2489
by: Johnathan Doe | last post by:
Hi, I am having problems understanding bitwise idioms that I see frequently in source code. I understand that they work at the individual bit level and that 0 | 1 = 1, 1 & 1 = 1 and so on (understand what all the operators mean), but when I try bit banging code it inevitably fails to get the result I wanted. I am trying to put together a packet to query a DNS server. I understand there are potentially little vs. big-endian problems,...
2
3462
by: Steve Summit | last post by:
-----BEGIN PGP SIGNED MESSAGE----- It's often explained that the reason for some of the imprecision in C's definition is so that C can be implemented on different kinds of machines -- say, those with 2's complement, 1's complement, or sign-magnitude arithmetic. But the followup remark is sometimes also made that the choice of arithmetic isn't completely unconstrained, since the bitwise operators seem to presume a base-2 machine.
9
7024
by: Christopher Weaver | last post by:
I know that the bitwise AND of 8 and 4 will return 0 or false and the bitwise AND of 8 and 9 will return 1 or true but I don't know how to write the synax for it in C#. I have a value that ranges from 0 to 15 and I need to compare it to 15 in order to find if it contains the values 1, 2, 4, or 8. To represent it more graphically, the value 1010 when ANDed with 1000 or 0010 will produce either true or a value greater than 0. I believe...
3
12488
by: shdwsclan | last post by:
I am native to various languages but bitwise operators just kill me. I see how much I take object oriented languages for granted. I like all the other c derivitives but ANSI C is making me loose my hair....especially ANSI C's bitwise operators..... For reference i come from the (Java/C#/C++) realm and was never forced to use these. Many people dont understand these....I tried to make sense....I know the truth tables...and I can do simple...
5
5780
by: noridotjabi | last post by:
I'm learning to program in C and any tutorial or book that I read likes to briefly touch on birdies operators and then move on without giving any sort of example application of them. Call me what you will but I cannot seem to see the purpose for bitwise operators. Especially the operators bitwise OR ( | ) and bitwise AND ( & ), I'm just not getting it. I have searched around and really haven't found anything that gave explanation to why...
3
2379
by: Jay Ruyle | last post by:
I'm trying to figure out a way to list several items in a listbox and let the user select any number of items in the listbox. I have tried to code in the items as bitwise items but all it stores in the database is the top item in the listbox. How can I get the listbox to add all the bit values to be stored and also come out properly when viewed later? Example:
2
3827
by: in_tyrannos | last post by:
First, the file is read to the buffer. Now, I want to access this buffer in the following way. Start index is defined as i = j*size(byte) +k*size(bit) = j*8+k*1. The length of the sequence to be read from buffer is defined similarly. So basically, I want to read sequence of bits from buffer at specified bit-location. Further, I want to do this efficiently. For example, start index is 13. The length of sequence of bits to be read is 17....
0
8427
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, 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...
0
8851
Oralloy
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...
1
8525
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,...
1
6179
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5649
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4175
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
4335
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2750
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
1737
bsmnconsultancy
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...

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.