LinuxQuestions.org
Latest LQ Deal: Latest LQ Deals
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 03-12-2007, 01:51 PM   #1
User Name.
Member
 
Registered: Aug 2006
Distribution: Ubuntu 8.04
Posts: 178

Rep: Reputation: 30
Bitwise Operators - C Programming


I am reading K&R, and I don't exactly under stand the |, ^, &, and ~. Are these entirely necessary to know? How often do I need to know these?
 
Old 03-12-2007, 02:21 PM   #2
oneandoneis2
Senior Member
 
Registered: Nov 2003
Location: London, England
Distribution: Ubuntu
Posts: 1,460

Rep: Reputation: 48
When I was working through K&R, bitwise operators gave me some trouble too. When I'd sorted it all out, I wrote this article as a summary. You might find it of some use..
 
Old 03-12-2007, 02:29 PM   #3
taylor_venable
Member
 
Registered: Jun 2005
Location: Indiana, USA
Distribution: OpenBSD, Ubuntu
Posts: 892

Rep: Reputation: 43
You'll need to use these basically anytime you start dealing with raw bits. In C, this can happen a lot, depending on what you're doing. Maybe you want to check some data that's come over the network to find out if it has a flag set -- use bitwise AND. Or you're passing some options to an OpenGL function -- use bitwise OR. It comes up a lot, actually, if you start doing such low-level things. And if you're not doing low-level things... well, what are you using C for?!
 
Old 03-12-2007, 02:34 PM   #4
osor
HCL Maintainer
 
Registered: Jan 2006
Distribution: (H)LFS, Gentoo
Posts: 2,450

Rep: Reputation: 78
Quote:
Originally Posted by User Name.
I am reading K&R, and I don't exactly under stand the |, ^, &, and ~. Are these entirely necessary to know? How often do I need to know these?
They are not entirely necessary to know, but you will read code using them (many times for flags). What exactly don’t you understand about them?
Code:
unsigned int a = 42;
unsigned int b = 24;
printf("%u\n", a | b);
printf("%u\n", a & b);
printf("%u\n", a ^ b);
Code:
In binary, a = 101010
and        b = 011000

So for |, you OR each bit
i.e., a | b == 111010 == 58

For &, you AND each bit,
i.e., a & b == 001000 == 8

For ^, you XOR each bit
i.e., a ^ b == 110010 == 50
 
Old 03-12-2007, 03:04 PM   #5
User Name.
Member
 
Registered: Aug 2006
Distribution: Ubuntu 8.04
Posts: 178

Original Poster
Rep: Reputation: 30
Quote:
Originally Posted by oneandoneis2
When I was working through K&R, bitwise operators gave me some trouble too. When I'd sorted it all out, I wrote this article as a summary. You might find it of some use..
That article helped alot, thanks.

osor: That was also helpful.

Thanks for the fast and helpful replies.
 
Old 03-12-2007, 10:41 PM   #6
graemef
Senior Member
 
Registered: Nov 2005
Location: Hanoi
Distribution: Fedora 13, Ubuntu 10.04
Posts: 2,379

Rep: Reputation: 148Reputation: 148
Just so that you know your efforts to understand bitwise operators are not wasted, they are implemented in most other languages as well .
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Inconsistent bitwise operator behaviour in C oneandoneis2 Programming 4 02-08-2007 03:20 AM
Bitwise >> formula concept... debiant Programming 10 08-31-2006 08:28 PM
Operators *^ and >> GodSendDeath Programming 4 11-01-2004 09:47 PM
about bitwise operators? eshwar_ind Programming 17 10-25-2004 02:13 AM
Bitwise operators kamransoomro84 Programming 8 04-22-2004 10:46 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 09:25 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration