473,387 Members | 3,750 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,387 software developers and data experts.

how is line 2 & 3 getting the second and first bit?

Can some one explain how it's get the first and second bit, so, how is (i&2)/2 getting the second bit ?

Expand|Select|Wrap|Line Numbers
  1. for(i=0; i < 4; i++) {     
  2.  bit_a = (i & 2) / 2; // Get the second bit.     
  3.  bit_b = (i & 1);     // Get the first bit.      
  4. printf("%d | %d = %d\n", bit_a, bit_b, bit_a | bit_b);  
  5.  } 
  6.  
Apr 18 '15 #1
6 1364
weaknessforcats
9,208 Expert Mod 8TB
OK.

Let's assume i is 8 bits to keep things simple. Let's also assume i has a value of 245 which is 11110101 in binary.

The bits are numbered 0 to 7 from right to left:

11110101
..........^....... bit 2

Now you can AND this number with 00000100. This is called a mask.

11110101
00000100
--------------- AND
00000100

So the result of the AND will have a 1 when the number AND the mask both have a 1 in the same bit position.

In this case the result of the AND is equal to the mask. This means that bit 2 is 1 in the variable i.

Expand|Select|Wrap|Line Numbers
  1. unsigned int i = 245;
  2. unsigned int mask = 4;
  3. unsigned int result;
  4.  
  5. result = i & mask;   //The & is the bitwise AND operator
  6.  
  7. if (result == mask)
  8. {
  9.     //bit 2 is ON
  10.  
Apr 18 '15 #2
Ok, im still a little confused tho, sorry if this is frustrating For you!!!
Expand|Select|Wrap|Line Numbers
  1.  bit_a = (i & 2) / 2; // Get the second bit.   
  2.  
so keeping things simple. This is comparing
0000 0000
&
0000 0010 and the / 2 is saying look at the 2ND bit?
Apr 18 '15 #3
Sorry I posted a reply but did not post to your answer. Could you check my reply out and see if I'm understanding this correctly
Apr 18 '15 #4
weaknessforcats
9,208 Expert Mod 8TB
In this code:

Expand|Select|Wrap|Line Numbers
  1. bit_a = (i & 2) / 2; // Get the second bit.   
it says get the second bit. 2 is 00000010. The 1 is in bit 1, not bit 2. It is bit 1 because the position of the bit is 2 to the 1st power.

The bit on the far right is bit 0. To the left of bit 0 is bit 1.

The bit is either ON (1) or OFF(0).


Now look at i & 2:


Let's look at the possible conditions

00000010

or

00000000

If you AND with 2 (00000010) you get:

00000010
00000010
.................AND
00000010

or you get

00000000
00000010
.................AND
00000000


In the first case the result of the AND is 00000010, which is 2. i & 2 becomes 2. So now you take 2 / 2 which is 1. Hence the bit is ON.

The other case is 00000000 which is 0. Then 0/2 is 0. Hence the bit is OFF.
Apr 18 '15 #5
Ok so, let's say i =3

Bit_a
0000 0011
&
0000 0010
------------
0000 0010 = (2)/2 = 1
So on the final iteration
bit_a = 1

Bit_b
0000 0011
&
0000 0001
-----------
0000 0001 = 1
Bit_b = 1

And then in the print statement compares bit_a , bit_b using the OR operator

So...
0000 0001 bit_a
or
0000 0001 bit_b
------------
0000 0001 so the result of the printf in the final iteration should be 1 ?

Someone on another forum, as far as I understood it, was saying that the /2 is a different form of the shift operator, but it's really just the result of AND divided by 2. And the mask in this code would be the 2 for bit_a and 1 for bit_b. And the mask is used to look at a specific portion of 'i'. So in this case I'm looking at bit1 for the variable bit_a and bit0 for the variable bit_b.
Apr 19 '15 #6
weaknessforcats
9,208 Expert Mod 8TB
Yes, /2 is not the shift operator. It is the division operator.

The shift operator is << or >> to shift bits left or right.

As to your analysis on bit_a or bit_b. it looks correct. Congratulations.

So far you have been testing bits. Using similar approaches, you can set or reset bits.
Apr 19 '15 #7

Sign in to post your reply or Sign up for a free account.

Similar topics

19
by: Philipp Lenssen | last post by:
I don't know the English word, but I'm referring to the double-dash which is used to separate parts of a sentence. I'm using — so far. Now I saw – which is slightly shorter. Some sites use --. ...
9
by: Marchello | last post by:
Hi All. I will try to explain my question on following example: Having class CInteger (wraper on 'int' values): class CInteger { public: ..... void SetValueFromPoiner(int *new_value)
5
by: Scott Chang | last post by:
Hi all, I copied a set of VC++ version 6 source code of the 'cppdll'(2 projects) from a website and put the cppdll.cpp, cppdll.def, cpp.h, (as the 1st project) and test.cpp (as the 2nd project)...
2
by: Patrick.O.Ige | last post by:
I have a calendar and i'm getting the first day of the month by doing so below calendarstartdate.SelectedDate = Convert.ToDateTime(String.Format("{0:yyyy/MM/1}",DateTime.Now)); Are there better...
0
by: CAG | last post by:
Hi, I'm trying to call the c# dll from command line & also want to invoke the methods of dll from command line directly. I'm working on c# .net project. Can anyone help me. ...
10
by: cyberdwarf | last post by:
Anyone got a way to adjust line and box height to the varying size of an expanding detail section on a report? Thought I'd cracked it, but Access just bit me back! TIA Steve
1
by: Sjef | last post by:
Hello, I have read the listapart articles about tabbed navigation, sliding doors etc. But the thing is: I want a very simple, almost minimalistic tabbed navigation bar, completely shaped by css,...
2
by: programmerboy | last post by:
This questions is pretty easy, but let me ask since I wasn't able to figure it out. why I am always getting the first value from DropDownList when I have ddlStates.SelectedItem.Value; ...
0
by: Heshan Suri | last post by:
Hi, I have written a the following python code to analyse a function or a method. I am having a script which is having a python function (add) and a class (MyClass) with a method (multiply).When...
10
by: lvl 1 monk | last post by:
Hi all, I don't really have the money for the formal route so I went to the library and picked up an older c++ book and it seems I run into "antiquated headers" over and over again. The problem...
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: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
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
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
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:
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...

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.