Amazon.com Widgets What's the purpose of bit operators?
Welcome, Guest. Please login or register.
Did you miss your activation email?
June 19, 2013, 11:17:21 AM
Home Help Search chat Login Register   
News: Read this please.The Great Kangaroo Escape Looking for reviews of the 4th ed on Amazon!   Twitter:  @skochan
                     

+  Official Forum for Programming in Objective-C (the iPhone Programming Language) - Stephen Kochan
|-+  Old Stuff
| |-+  Chapter Study
| | |-+  Chapter 4 - Data Types and Expressions
| | | |-+  What's the purpose of bit operators?
Pages: [1] 2   Go Down
Print
Author Topic: What's the purpose of bit operators?  (Read 6164 times)
gabrielr
Newbie
*
Posts: 11






« on: February 27, 2009, 03:53:40 PM »

Most of the questions on this board seem to focus on higher-level stuff, but I'm on Chapter Four, and I'm confused about something. I can understand the bit operators introduced on page 67 and thereafter -- I mean, I understand how they each make changes to the binary representations of a value. What I don't understand is when or why I would use them -- why I'd want to mess with values in this particular way.

A quick explanation -- even something as simple as 'You'll use them in FOO operations, which are introduced in Chapter 11' -- would help me out. FYI, what little programming experience I have is with scripting languages, where I've never run into this bitwise stuff before.

Steve, thanks for the book (which is great so far), and for making yourself available here.
Logged
skochan
Administrator
Hero Member
*****
Posts: 3109







« Reply #1 on: February 27, 2009, 04:41:15 PM »

Hi,

Historically, bit operations were used to pack data into memory when it was dear.  Now the amount of memory available is often taken for granted, so most programmers don't need to use shifts, exclusive-ORs, etc.   If you work with a machine at a very low level, which the underlying C language was designed to allow, then you might look at a register and mask and shift some bits, for example.  So if you had an IP address like 192.168.2.1, where each number in the address is an integer from 0 to 255, you could represent an entire IP address in 32 bits.  If you had it stored in an unsigned variable called IPaddr, then this would isolate the first (i.e., leftmost) number in the address, which is 192:

Code: (Objective-C)
IPaddr >> 24

YES/NO or TRUE/FALSE can be represented in a single bit (either the bit is on or its off).  The Foundation comparison and search methods (you'll learn about this later) use a combination of bits to describe the options for a comparison and search.  For example in NSString.h, the value NSCaseInsensitiveSeach is set as the value 1 and NSBackwardsSearch to 4 (note that all the options represent a single bit and are set to a power of 2 so they can be combined).  To perform a case insensitive search starting from the end going backwards, the bitwise OR operator can be used to combine the two bits into a single value (historically referred to as a mask):

Code: (Objective-C)
NSCaseInsensitiveSearch | NSBackwardSearch

I hope this helps.  I put the bit operators in Chapter 4 for the sake of completeness, but perhaps should have given a better explanation of their use (or nonuse as the case may be).

Cheers,

Steve



« Last Edit: May 08, 2009, 04:13:39 AM by skochan » Logged
esc
Global Moderator
Full Member
*****
Posts: 230






« Reply #2 on: February 27, 2009, 05:07:19 PM »

Here's an example from UIView.h (UIKit):

enum {
    UIViewAutoresizingNone                 = 0,
    UIViewAutoresizingFlexibleLeftMargin   = 1 << 0,
    UIViewAutoresizingFlexibleWidth        = 1 << 1,
    UIViewAutoresizingFlexibleRightMargin  = 1 << 2,
    UIViewAutoresizingFlexibleTopMargin    = 1 << 3,
    UIViewAutoresizingFlexibleHeight       = 1 << 4,
    UIViewAutoresizingFlexibleBottomMargin = 1 << 5
};
typedef NSUInteger UIViewAutoresizing;

In your app, you may set a UILabel object called labelField like this:

labelField.autoresizingMask = (UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight);


Logged
gabrielr
Newbie
*
Posts: 11






« Reply #3 on: February 27, 2009, 05:16:37 PM »

Thank you for the replies. I'll keep that in the back of my head as I move on.
Logged
yo9481
Newbie
*
Posts: 1


Email




« Reply #4 on: March 24, 2009, 12:25:28 PM »

Hello,

I'm still having some difficulty understanding this concept.  I am completely new to programming (I last worked with BASIC in grade school).  I think the concept I'm missing for this section is how to know the bit representation of a variable.  I know it gets converted to binary for use in the computer system, but it is converted back to a normal form when outputted.  Is there an easy way to determine this binary representation?

I haven't had any trouble with the other concepts I've been learning in the book and am hoping that there is a simplistic answer for this as well.

Thanks so much for the help.  I think this forum is an incredible resource and adds so much value to the book.  It is all really appreciated!
Logged
skochan
Administrator
Hero Member
*****
Posts: 3109







« Reply #5 on: March 24, 2009, 01:16:35 PM »

If you're not fiddling with the particular bits themselves, you don't need to worry about how the value stored in a variable is internally represented.  The conversion that takes place on output is specified by the format characters in the NSLog format string.  If you want to display an integer specify %i or %d, it output the value in decimal, %o in octal, and %x in hexadecimal, for example.  But in any of these cases, the internal representation of the integer value is the same.

Hope this helps,

Steve Kochan
Logged
Robot-Scott
Jr. Member
**
Posts: 52







« Reply #6 on: April 10, 2009, 09:14:25 PM »

For me, the part about bit operators finally helped me understand subnet masks and hexadecimal.  I always wondered why Hexadecimal was base 16 and now I know that it is perfect for representing 4-bits of data.  And the subnet masks are something I never really understood (I work in IT so I have to deal with them a LOT) but now I understand what a bitwise AND operation is so it makes sense.  Kind of a roundabout way of learning but you wouldn't believe the joy I had in writing out the bitwise operations (and getting the right answers!) using hexadecimal.  Grin
Logged

"Think globally, act within local variable scope."
TheWoz
Newbie
*
Posts: 27






« Reply #7 on: May 03, 2009, 10:38:07 AM »

Isn't it better to learn about bitwise operators later in the book? In my opinion, chapter 4 is too soon to explain this. A lot of people will get overwhelmed at an early stage...
« Last Edit: May 03, 2009, 01:12:41 PM by TheWoz » Logged
TheWoz
Newbie
*
Posts: 27






« Reply #8 on: May 10, 2009, 12:35:38 AM »

Isn't it better to learn about bitwise operators later in the book? In my opinion, chapter 4 is too soon to explain this. A lot of people will get overwhelmed at an early stage...
...
Logged
yvonnef
Newbie
*
Posts: 9






« Reply #9 on: May 29, 2009, 04:52:33 PM »

I don't have a problem with why, when you stated their use in masking (I assume for photos). The truth tables are fine also. What I don't get is the resulting Hex "number". How do you get 0x15 &0x0c=0x04? Huh
Logged
skochan
Administrator
Hero Member
*****
Posts: 3109







« Reply #10 on: May 29, 2009, 05:51:01 PM »

0x15 = 0001 0101
0x0c = 0000 1100  &
------------------------
0x04 = 0000 0100


Cheers,

Steve Kochan
Logged
yvonnef
Newbie
*
Posts: 9






« Reply #11 on: May 29, 2009, 10:08:33 PM »

Clearly I'm just not seeing it. I've had years of engineering calculus, but I can't seem to figure out how you get the Hex numbers. I understand the table and how you turn the 1s to 0s from the examples. What I don't get is the Hex answers from those tables. I'm not seeing the pattern of how you can take 15 & 0c and end up with 04. I understand how you get the last line of 0000 0100, just not how that adds up to 04.

Sorry for being so dense on this issue.

UPDATE: I looked up the Hexadecimal chart on Wikipedia to figure out what you are talking about. I could not find one in the book, but may have just missed it. The only way I can see to come up with the final number from the 8 digits shown on the Truth Chart is to look it up on the Hex chart. Is there another way?
« Last Edit: May 29, 2009, 11:35:42 PM by yvonnef » Logged
jkiley
Newbie
*
Posts: 43



Email




« Reply #12 on: May 30, 2009, 07:21:03 AM »

Binary numbers (base-2) are laid out like decimal numbers (base-10), but we rarely thing about the structure this way. I'm using the caret symbol (i.e. "^") to denote exponents below.

In decimal, the right-most digit is the 10^0 place (i.e. 1 times the digit in that place).  The next digit is the 10^1 place (i.e. 10 times the digit in that place).

It is the same in binary, except the base is different.  The right-most place is 2^0, and the next two from the right are 2^1 and 2^2 (and so on).

In the binary number 0000 0100, the 2^0 and 2^1 places are 0.  The 2^2 place is 1, so 2^2 is 4 times the 1 in that place.  Hex (base-16) has enough digits to represent that 4 as a 4, so the hex number is 0x04 (i.e. 16^0 * 4).

I hope this helps.
Logged
yvonnef
Newbie
*
Posts: 9






« Reply #13 on: June 01, 2009, 02:21:36 PM »

That's a great help, thank you, but I will have to study it (there's a lot in that post to think through!). So how do you get 0c?
Logged
skochan
Administrator
Hero Member
*****
Posts: 3109







« Reply #14 on: June 01, 2009, 02:40:08 PM »

Each 4 bits represents a hexadecimal digit:

Binary  Hex  Dec
0000     0      0
0001     1      1
0010     2      2
0011     3      3
0100     4      4
0101     5      5
0110     6      6
0111     7      7
1000     8      8
1001     9      9
1010     A     10
1011     B     11
1100     C     12
1101     D     13
1110     E     14
1111     F     15

When you see a string of bits, you split them into these groups of 4, as is done in the text, for example:

0010  1000  1100 0000 =
  2        8        C      0       or 2BC0 hexadecimal.

Cheers,

Steve Kochan
« Last Edit: June 01, 2009, 02:43:17 PM by skochan » Logged
Pages: [1] 2   Go Up
Print
Jump to:  



Login with username, password and session length

Powered by MySQL Powered by PHP Powered by SMF 1.1.11 | SMF © 2006-2009, Simple Machines LLC Valid XHTML 1.0! Valid CSS!
Entire forum contents (c) 2009 classroomM.com. All rights reserved.