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

how important are bitwise operators?

I'm currently learning C with K&R and I was going over the
section with the Bitwise operators.
What exactly are usefull uses for them?
Are they really important?
thanks in advance.
Nov 13 '05 #1
8 2196
Andy <ma******@bellsouth.net> wrote in
news:C7******************@bignews6.bellsouth.net:
I'm currently learning C with K&R and I was going over the
section with the Bitwise operators.
What exactly are usefull uses for them?
When you want to pack a bunch of statii into a single integer. When you
want to check alignment of a pointer.

e.g.

#define FLAG_1 (1 << 0)
#define FLAG_2 (1 << 1)
#define FLAG_3 (1 << 2)

unsigned int flags;

if (flags & (FLAG_1 | FLAG_3))
printf("Flag 1 and 3 are set\n");
char *ptr = malloc(1024);

if (ptr && !((unsigned int) ptr & 0xF))
printf("Got a pointer with alignment I can use\n");
Are they really important?


Very!

--
- Mark ->
--
Nov 13 '05 #2
Andy writes:
I'm currently learning C with K&R and I was going over the
section with the Bitwise operators.
What exactly are usefull uses for them?
Are they really important?


They are important but I wouldn't get stressed out over them if I were you.
If you can't appreciate them at the moment, let it pass, if you keep
programming their usage will become clear to you sooner or later.

One example: the exclusive or operator is almost essential for cryptographic
work.
Nov 13 '05 #3
"Mark A. Odell" <no****@embeddedfw.com> wrote in message
news:Xn********************************@130.133.1. 4...
Andy <ma******@bellsouth.net> wrote in
news:C7******************@bignews6.bellsouth.net:
I'm currently learning C with K&R and I was going over the
section with the Bitwise operators.
What exactly are usefull uses for them?


When you want to pack a bunch of statii into a single integer. When you
want to check alignment of a pointer.

e.g.

#define FLAG_1 (1 << 0)
#define FLAG_2 (1 << 1)
#define FLAG_3 (1 << 2)

unsigned int flags;

if (flags & (FLAG_1 | FLAG_3))
printf("Flag 1 and 3 are set\n");
char *ptr = malloc(1024);

if (ptr && !((unsigned int) ptr & 0xF))
printf("Got a pointer with alignment I can use\n");

Out of curiosity, under what circumstances would conforming
implementation return non-null unsuitably aligned pointer
(for legal uses of such pointer)? Thanks.
Nov 13 '05 #4

On Wed, 12 Nov 2003, nobody wrote:

"Mark A. Odell" <no****@embeddedfw.com> wrote...

char *ptr = malloc(1024);

if (ptr && !((unsigned int) ptr & 0xF))
printf("Got a pointer with alignment I can use\n");


Out of curiosity, under what circumstances would conforming
implementation return non-null unsuitably aligned pointer
(for legal uses of such pointer)? Thanks.


First of all, we should note that Mark's trying to
cast 'ptr' from 'char *' to 'unsigned int', which right
off the bat won't necessarily work the way anyone might
expect it to.
Then note that he's ANDing it with 0xF to "check"
its alignment, which only works if properly aligned
pointers on your system happen to be converted to
unsigned ints with the lower four bits cleared. That's
not a valid assumption on the platform I'm familiar
with.
Finally, to answer nobody's question, ;-) a conforming
implementation *cannot* return an "unsuitably aligned"
pointer from a conforming call to 'malloc' (i.e., a call
that doesn't invoke undefined behavior all by itself).
'malloc' always returns pointers suitably aligned for
any type, which means you never have to do the sort of
non-portable "checking" Mark described above.

<OT>
Maybe 'sbrk' or the equivalent on some systems
doesn't return nicely aligned chunks of memory, so
you'd have to test there. But that's off-topic
in comp.lang.c. Ask a systems group if you *really*
want to know. (Really OT: And if you've been keeping
track of my grammar, you'll know what this last sentence
is about.)
</OT>

-Arthur,
ungrammatical to the last
Nov 13 '05 #5

"Arthur J. O'Dwyer" <aj*@nospam.andrew.cmu.edu> wrote in message
news:Pine.LNX.4.58-035.0311120045400
Finally, to answer nobody's question, ;-) a conforming
implementation *cannot* return an "unsuitably aligned"
pointer from a conforming call to 'malloc' (i.e., a call
that doesn't invoke undefined behavior all by itself).
'malloc' always returns pointers suitably aligned for
any type, which means you never have to do the sort of
non-portable "checking" Mark described above.


Depending on your definition of suitable. As I understand, for
implementations that can use unaligned pointers, but perform slower with
them, they are allowed. As the C standard makes no claims to the speed of
operations, that could be true.

If I am writing a program that needs to be fast, I would call the
suboptimally aligned pointer unsuitable.

-- glen
Nov 13 '05 #6
"Arthur J. O'Dwyer" <aj*@nospam.andrew.cmu.edu> wrote in message
news:Pi***********************************@unix45. andrew.cmu.edu...

On Wed, 12 Nov 2003, nobody wrote:

"Mark A. Odell" <no****@embeddedfw.com> wrote...

char *ptr = malloc(1024);

if (ptr && !((unsigned int) ptr & 0xF))
printf("Got a pointer with alignment I can use\n");
Out of curiosity, under what circumstances would conforming
implementation return non-null unsuitably aligned pointer
(for legal uses of such pointer)? Thanks.


First of all, we should note that Mark's trying to
cast 'ptr' from 'char *' to 'unsigned int', which right
off the bat won't necessarily work the way anyone might
expect it to.
Then note that he's ANDing it with 0xF to "check"
its alignment, which only works if properly aligned
pointers on your system happen to be converted to
unsigned ints with the lower four bits cleared. That's
not a valid assumption on the platform I'm familiar
with.


Thanks for an answer.
Yes, I've noted it, just didn't want to comment on it.
/* Also, I personally would use e.g
#define FLAG_1 0x01
instead of
#define FLAG_1 (1 << 0) */
Finally, to answer nobody's question, ;-) a conforming
implementation *cannot* return an "unsuitably aligned"
pointer from a conforming call to 'malloc' (i.e., a call
that doesn't invoke undefined behavior all by itself).
'malloc' always returns pointers suitably aligned for
any type, which means you never have to do the sort of
non-portable "checking" Mark described above.

I just wanted to make sure that I didn't (yet again) mis-read
N869. Though even I agree with final answer (bitwise operators
are very important), it seems to me that given examples were
not the best choice for supporting this answer (not that I
have any better).
Nov 13 '05 #7
"Glen Herrmannsfeldt" <ga*@ugcs.caltech.edu> wrote in message
news:Xhlsb.129833$9E1.656939@attbi_s52...

"Arthur J. O'Dwyer" <aj*@nospam.andrew.cmu.edu> wrote in message
news:Pine.LNX.4.58-035.0311120045400
Finally, to answer nobody's question, ;-) a conforming
implementation *cannot* return an "unsuitably aligned"
pointer from a conforming call to 'malloc' (i.e., a call
that doesn't invoke undefined behavior all by itself).
'malloc' always returns pointers suitably aligned for
any type, which means you never have to do the sort of
non-portable "checking" Mark described above.
Depending on your definition of suitable.


Not really. I meant "suitable aligned" as meant by 7.20.3p1,
whatever that means (no, it's not a question).
As I understand, for
implementations that can use unaligned pointers, but perform slower with
them, they are allowed. As the C standard makes no claims to the speed of operations, that could be true.

If I am writing a program that needs to be fast, I would call the
suboptimally aligned pointer unsuitable.

Well, difference of opinion. Considering topicality, I would
call it 'inconveniently' aligned. Can't imagine how one could
force malloc() to return pointer with specific alignment.
Nov 13 '05 #8
"nobody" <no****@nowhere.non> wrote in
news:BU********************@twister01.bloor.is.net .cable.rogers.com:
Thanks for an answer.
Yes, I've noted it, just didn't want to comment on it.
/* Also, I personally would use e.g
#define FLAG_1 0x01
instead of
#define FLAG_1 (1 << 0) */


You know when you have a list of them, it becomes more clear my way (IMHO)
especially with the PowerPC and IBM's damn backwards bits. E.g. if you
want to create masks for an IBM PowerPC register using IBM's bit notation
from the data book without translating in your brain you just do this:

/* flag reg. IBM datasheet
** width size bit position
** - 1
** | | |
** v v v */
#define CPC0_CR0_TRE (1 << 31 - 4)
#define CPC0_CR0_G10E (1 << 31 - 5)
#define CPC0_CR0_G11E (1 << 31 - 6)
#define CPC0_CR0_G12E (1 << 31 - 7)
#define CPC0_CR0_G13E (1 << 31 - 8)
#define CPC0_CR0_G14E (1 << 31 - 9)
#define CPC0_CR0_G15E (1 << 31 - 10)
#define CPC0_CR0_G16E (1 << 31 - 11)
#define CPC0_CR0_G17E (1 << 31 - 12)
#define CPC0_CR0_G18E (1 << 31 - 13)
#define CPC0_CR0_G19E (1 << 31 - 14)
#define CPC0_CR0_G20E (1 << 31 - 15)
#define CPC0_CR0_G21E (1 << 31 - 16)
#define CPC0_CR0_G22E (1 << 31 - 17)
#define CPC0_CR0_G23E (1 << 31 - 18)
#define CPC0_CR0_DCS (1 << 31 - 19)
#define CPC0_CR0_RDS (1 << 31 - 20)
#define CPC0_CR0_DTE (1 << 31 - 21)
#define CPC0_CR0_DRE (1 << 31 - 22)
#define CPC0_CR0_DAEC (1 << 31 - 23)
#define CPC0_CR0_U0EC (1 << 31 - 24)
#define CPC0_CR0_U1EC (1 << 31 - 25)

Maybe you can quickly translate IBM's bit 4 into 0x0800'0000 but I cannot,
certainly not reliably.

--
- Mark ->
--
Nov 13 '05 #9

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

Similar topics

9
by: Michael B. Trausch | last post by:
I have a question regarding bitwise operators, I've been trying to figure this out for about two days now, and I just can't seem to get it. What I'm trying to do is use a variable to hold a bitmask...
11
by: Randell D. | last post by:
Why would one use bitwise operators? I can program in various languages in some shape or form (C++, PHP, some scripting) and I've heard/seen bitwise operators before, but never understood why...
4
by: Mike Hodkin | last post by:
As a beginning student of C++, books reference "bitwise operators" and give brief examples, but I have not read a good explanation of what they are used for. One reference mentioned that they are...
6
by: jas_lx | last post by:
The basic understanding of what bitwise operators (& ^ | >> << ) comes fairly simple, as long as one has a fundamental understanding of bits, bytes and binary. Having done some Win32...
37
by: James Radke | last post by:
Hello, I found some code that I could use in my application on the web, but it is written in C#, and I would like to convert it to VB. And I am having problems with one section. Is there...
5
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...
2
by: Mark Rae | last post by:
Hi, This isn't *specifically* an ASP.NET question, so I've also posted it in the ADO.NET group - however, it's not too far off-topic... Imagine a SQL Server 2005 database with a table with an...
29
by: Carl Banks | last post by:
Anyone with me here? (I know the deadline for P3 PEPs has passed; this is just talk.) Not many people are bit-fiddling these days. One of the main uses of bit fields is flags, but that's not...
16
by: Santhosh | last post by:
Hi to all, How the individual digits of a number can be obtained using the bitwise operators alone.Is it possible to do it ? If we have n = 34 Result has to be 3,4. Thanks a billion for...
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: 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
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: 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
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
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,...
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
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...

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.