Amazon.com Widgets What's the purpose of bit operators?
Welcome, Guest. Please login or register.
Did you miss your activation email?
October 19, 2014, 09:26:45 PM
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 8716 times)
yvonnef
Newbie
*
Posts: 9






Reply #15 on: June 01, 2009, 03:47:25 PM サ

To me that chart really helps you to understand how the ending values are derived. I think that would be a great addition to the text (if it's not in there already and I just missed it Smiley ) in the next version.

I went back to the text to where these concepts are introduced which is on page 50 of the current version if anyone wants to review.

Thanks for clearing that up for me!

Why is Octal Base 8 notation important in programming?

Logged
Deaway
Newbie
*
Posts: 8


Email




Reply #16 on: July 23, 2009, 11:19:45 AM サ


In another post I was wondering how I could do huge multiplications. Unsigned long long int wasn't of any help, so Steve suggested to implement some multiplication algorithm.

I did a little of searching and found that one way was to multiply the binary equivalent.

So if you want to multply 11 by 7, in base 2 you have 1011 * 111

It goes this way, you multiply the old fashion way

     1011
       111        multiply
------------
     1011
   10110        add them up
 101100
------------
1001101

Notice how the multiplicand (11, 1011 in base 2) shifts one position every time it is mulplied by 1. So it becomes a matter of AND and OR.

Now I haven't programmed it myself (I'm still new to programming), but this is one example where it could be useful. This by no way the best way to do huge multiplication. Someone else could confirm this, but maybe this is the basic way X-code do the small multiplication in the programs you write.

Hope that was clear enough  Undecided I'm still learning and trying to do some connection between the new knowledge
Logged
skochan
Administrator
Hero Member
*****
Posts: 3114







Reply #17 on: July 23, 2009, 11:42:54 AM サ

Thanks for the post.

No, the machine actually has an integer multiply instruction.  If you wrote this:

Code: (Objective-C)
int i, j, k;
...
k = i * j;
...

and then examined the assembly code (that's the code your source code gets compiled into and that typically maps one-for-one to actual processor instructions) that gets generated for the multiplication statement, it would like something like this:

Code: (Objective-C)
movl	-20(%ebp), %eax
imull -16(%ebp), %eax
movl %eax, -12(%ebp)

This says to move the value of i into a register (it's a local variable sitting on the stack and the movl instruction references the variable as a (negative 20) offset from the stack), multiply the value in the register by j, and then move the result into the variable k.

Haha.  One side point post followed by another.  Forum members---you can just skip this!  :-)

Cheers,

Steve Kochan
Last Edit: July 23, 2009, 11:47:47 AM by skochan Logged
ChrisJS
Newbie
*
Posts: 5






Reply #18 on: August 12, 2009, 09:56:06 AM サ

I registered just so I could post about this subject.  Makes me feel better when I saw this was the top post.

First off, great book so far.  I've understood all the concepts right up to this on page 67, where I'm now completely stuck.  I understand binary and hex, but I don't understand this at the least.

I've read all the posts in this thread topic, and most of it sounds gibberish.  I think this is a little too advanced for this chapter, and though I saw mentioned that this was added for "completeness," I think there could have been much better ways of going about this.  How about "Bit Operators fall into this category, but will be explained later in Chapter #"

I don't know whether to keep going on with the book and ignore this for now, or sit here stressing in my frustration.

I'm very stuck now, as I'm sure others are.  I hope the rest of the book is as page 1-66 are (within grasp).
Last Edit: August 12, 2009, 09:58:55 AM by ChrisJS Logged
Avizzv92
Jr. Member
**
Posts: 63


Email




Reply #19 on: August 12, 2009, 10:17:29 AM サ

I registered just so I could post about this subject.  Makes me feel better when I saw this was the top post.

First off, great book so far.  I've understood all the concepts right up to this on page 67, where I'm now completely stuck.  I understand binary and hex, but I don't understand this at the least.

I've read all the posts in this thread topic, and most of it sounds gibberish.  I think this is a little too advanced for this chapter, and though I saw mentioned that this was added for "completeness," I think there could have been much better ways of going about this.  How about "Bit Operators fall into this category, but will be explained later in Chapter #"

I don't know whether to keep going on with the book and ignore this for now, or sit here stressing in my frustration.

I'm very stuck now, as I'm sure others are.  I hope the rest of the book is as page 1-66 are (within grasp).

I had some difficulties aswell, but it helped me to reread everything a few times before moving on. For the most part the rest of book doesn't require complete understanding of the topic. Some parts can be equally difficult at times but definitely with grasps.
Logged
ChrisJS
Newbie
*
Posts: 5






Reply #20 on: August 12, 2009, 02:45:27 PM サ

I had some difficulties aswell, but it helped me to reread everything a few times before moving on. For the most part the rest of book doesn't require complete understanding of the topic. Some parts can be equally difficult at times but definitely with grasps.

Thank you for the reply Avizzv92...
I will reread for sure, as I have a few times already, hopefully it becomes more clear! Smiley

QUOTE FROM PAGE 68,

Quote
For example, if w1 and w2 are defined as short ints, and w1 is set equal to hexadecimal
15 and w2 is set equal to hexadecimal 0c, then the following C statement assigns the value
0x04 to w3:
w3 = w1 & w2;
You can see this more easily by treating the values of w1, w2, and w3 as binary numbers.
Assume that you are dealing with a short int size of 16 bits:
w1    0000 0000 0001 0101     0x15
w2    0000 0000 0000 1100   & 0x0c
覧覧覧覧覧覧覧覧覧-
w3    0000 0000 0000 0100     0x04

Bitwise ANDing is frequently used for masking operations. That is, this operator can be used to easily set specific bits of a data item to 0. For example, the following statement asigns to w3 the value of w1 bitwise ANDed with the constant 3.

w3 = w1 & 3;

This has the effect of setting all the bits in w3, other than the rightmost 2 bits, to 0, and of preserving the rightmost 2 bits from w1.

Just before those last two paragraphs, w3 was set to 0x04 (dec: 4), so unless it's assumed that now w3 is actually 0x03 and just not mentioned (if this is the case, it should probably be mentioned as it's confusing), then shouldn't the value end up as 0?

example (w3 = w1 & 3;):

w1 = 4 (... 0100)
constant = 3 (... 0011)
ANDed w3: ... 0000

w1 = 3 (... 0011)
constant = 3 (... 0011)
ANDed w3: ... 0011

Am I wrong here?
Last Edit: August 12, 2009, 02:58:23 PM by ChrisJS Logged
rgronlie
Global Moderator
Full Member
*****
Posts: 212







Reply #21 on: August 12, 2009, 03:22:36 PM サ

Ignore what the value of w3 was previously since a new value is being assigned to it

w3 = w1 & 3;

you would be correct if the statement was

w3 = w3 & 3;
Logged

Sanity: Minds are like parachutes. Just because you've lost yours doesn't mean you can borrow mine.
skochan
Administrator
Hero Member
*****
Posts: 3114







Reply #22 on: August 12, 2009, 06:19:43 PM サ

Believe me, I have listened to everyone's feedback on that topic and it will be addressed if I write another edition.  Quite honestly, this has a little to do with my longevity in the industry.  When I started programming, everything was about assembly language programming, conserving memory, manipulating bits, masking, etc.   So there's a little bit of a generational gap here in my presentation of this topic that I missed.   Embarrassed

For now, as noted elsewhere, anyone finding the discussion on bit operations a little esoteric please just skip the section and don't worry about it.  It was placed there for sake of organization and completeness (presenting the operators in one chapter), and that was a mistake on my part.

Cheers,

Steve Kochan
Logged
quid squid
Newbie
*
Posts: 1


Email




Reply #23 on: August 18, 2009, 01:02:18 PM サ

Believe me, I have listened to everyone's feedback on that topic and it will be addressed if I write another edition.  Quite honestly, this has a little to do with my longevity in the industry.  When I started programming, everything was about assembly language programming, conserving memory, manipulating bits, masking, etc.   So there's a little bit of a generational gap here in my presentation of this topic that I missed.   Embarrassed

For now, as noted elsewhere, anyone finding the discussion on bit operations a little esoteric please just skip the section and don't worry about it.  It was placed there for sake of organization and completeness (presenting the operators in one chapter), and that was a mistake on my part.

Cheers,

Steve Kochan

I just wanted to say I came here specifically research this topic, and as soon as I saw this thread and read through it I completely understood what I was looking at in the book. This section was one of the reasons I restarted the book, and going through a second time more carefully definitely helped me as well. The chart helped, but so did everyone's explanations.

so I say thank you everyone, and thank you Steve for having a place like this to help better understand any issues we are having with programming Obj-C. on to chapter 5 I go Smiley
Logged
Bo_Blomgren
Newbie
*
Posts: 7


Email




Reply #24 on: December 19, 2009, 04:37:00 PM サ

It was really helpful to read the discussion about bit operators. I am on that chapter myself, and felt quite stuck. Then it is nice to be able to find tips and answers in the forum. Thanks, everyone!
Bo
Logged
My2CentsWorth
Newbie
*
Posts: 49


Mark






Reply #25 on: March 14, 2010, 08:56:57 PM サ

Believe me, I have listened to everyone's feedback on that topic and it will be addressed if I write another edition.  Quite honestly, this has a little to do with my longevity in the industry.  When I started programming, everything was about assembly language programming, conserving memory, manipulating bits, masking, etc.   So there's a little bit of a generational gap here in my presentation of this topic that I missed.   Embarrassed

For now, as noted elsewhere, anyone finding the discussion on bit operations a little esoteric please just skip the section and don't worry about it.  It was placed there for sake of organization and completeness (presenting the operators in one chapter), and that was a mistake on my part.

Cheers,

Steve Kochan

Thank God!
I will definitely skip this section as you all are so far above my ability to understand this and I don't think I want to even try. I was having problems with some aspects of learning this book so decided to just sit down and read it like a regular book to get an overview of everything, and then go back and do the studies one chapter at a time. I am just starting chapter 6 now, and everything is making way more sense to me now, but this part I reread 6 times and then came here for some clarification. I am going to act like these 5 pages don't exist. I don't want anyone to post against this to try to explain this to me, as I don't think it's possible, I am just glad to see I can just ignore it. I love this forum as a reference to the book, what an awesome idea. Thanks again.
Logged

Donate your unused CPU time to better the world we live in @ http://www.worldcommunitygrid.org/
pharmx
Newbie
*
Posts: 8






Reply #26 on: April 07, 2010, 02:12:12 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

Hi Steve,

While I understood this section on a conceptual level, my eyes started glazing over as I read this part in the book, so I skimmed over most of it.  For me it seemed like trying to learn the molecular structure of sugar before using it in a recipe to make something.  I'm hoping to one day go back and get a firm grasp on the topic, so that I feel comfortable working with bit operators when necessary.

In the meantime, I have a question regarding your example:

"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."


Shouldn't it be "28C0" instead of "2BC0" ?
Logged
skochan
Administrator
Hero Member
*****
Posts: 3114







Reply #27 on: April 07, 2010, 02:20:47 PM サ

Yes, that's a typo.

Cheers,

Steve Kochan
Logged
MarkCooperstein
Newbie
*
Posts: 19


Email




Reply #28 on: April 14, 2011, 10:35:04 AM サ

yvonnef - in case you still are unclear as to how you get 0x4 from 羨ND段ng 0x15 with 0x0c, look at Steve痴 solution below... He took hex 15 (decimal 21 or binary 10101) and 羨ND弾d it to hex C (decimal 12 or binary 1100). The AND operation says 疎 bit that is set (1) ANDed with another bit that is set (1) gives a result of 1, otherwise the result is 0. Another way of saying this is that both bits have to be 1 to get a TRUE or 1 result. The OR operation says if either (or both) bit is a 1 the result is a 1 else 0. Exclusive OR (XOR) is similar to the OR operation but says: if one or the other but not both is 壮et (1) then it痴 TRUE else FALSE or 0.

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

Really, bit operations are handy when you池e dealing with hardware on a device driver level for the simple reason that programming the device at the 鼠owest software level usually gives you the best control and speed.  Many devices store data in hardware registers and it痴 up to you to decide how to deal with it.  This is a complex programming task that is well outside the scope of this class so don稚 obsess about it!

Cheers - Mark
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.