473,566 Members | 2,785 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Using bitshift in assigning a number to a variable?

Is there any benefit to declaring a variable:

x = (1<<12);

instead of:

x = 4096;

(besides the geek appeal of course ;-) )

I have seen these kind of declarations come up in some code I have been
fiddling with and was wondering if there was any tangible benefit, (I have
also seen stuff declared as hex, is there any benefit to that either).

---
bollod
Nov 14 '05 #1
12 3927
bollod wrote:

Is there any benefit to declaring a variable:

x = (1<<12);

instead of:

x = 4096;

(besides the geek appeal of course ;-) )

I have seen these kind of declarations come up
in some code I have been
fiddling with and was wondering if there was any tangible benefit,
(I have also seen stuff declared as hex,
is there any benefit to that either).


It's easier to visualise the binary representation
of the values that way,
which is especially good if your numbers are masks.

--
pete
Nov 14 '05 #2
On Fri, 26 Dec 2003 04:03:30 +0000, pete wrote:
bollod wrote:

Is there any benefit to declaring a variable:

x = (1<<12);

instead of:

x = 4096;

(besides the geek appeal of course ;-) )

I have seen these kind of declarations come up
in some code I have been
fiddling with and was wondering if there was any tangible benefit,
(I have also seen stuff declared as hex,
is there any benefit to that either).


It's easier to visualise the binary representation
of the values that way,
which is especially good if your numbers are masks.


That makes sense.

What would you do if you needed to create the mask '10101010' (besides
foo=170 or foo=0xA4)?

---
bollod
Nov 14 '05 #3
bollod wrote:

On Fri, 26 Dec 2003 04:03:30 +0000, pete wrote:
bollod wrote:

Is there any benefit to declaring a variable:

x = (1<<12);

instead of:

x = 4096;

(besides the geek appeal of course ;-) )

I have seen these kind of declarations come up
in some code I have been
fiddling with and was wondering if there was any tangible benefit,
(I have also seen stuff declared as hex,
is there any benefit to that either).


It's easier to visualise the binary representation
of the values that way,
which is especially good if your numbers are masks.


That makes sense.

What would you do if you needed to create the mask '10101010' (besides
foo=170 or foo=0xA4)?


'10101010' looks more like 0xaa to me.

The shift notation is better
when the right operand of the shift operator
is an enum or a macro identifier which indicates which flag
the mask applies to.

--
pete
Nov 14 '05 #4
bollod wrote:
What would you do if you needed to create the mask '10101010' (besides
foo=170 or foo=0xA4)?


I would write
#define SOME_MASK (1<<7 | 1<<5 | 1<<3 | 1<<1)
or
const int SOME_MASK=1<<7 | 1<<5 | 1<<3 | 1<<1;

--
Ro************* @rbdev.net
Nov 14 '05 #5
in comp.lang.c i read:
What would you do if you needed to create the mask '10101010' (besides
foo=170 or foo=0xA4)?


correction: foo=0xAA. also foo=0252 and foo=strtol("101 01010", 0, 2),
though the later cannot be used at file scope.

--
a signature
Nov 14 '05 #6
On Thu, 25 Dec 2003, bollod wrote:
Is there any benefit to declaring a variable:

x = (1<<12);

instead of:

x = 4096;

(besides the geek appeal of course ;-) )

I have seen these kind of declarations come up in some code I have been
fiddling with and was wondering if there was any tangible benefit, (I have
also seen stuff declared as hex, is there any benefit to that either).


I'll answer the easier question first, there is a benefit to hexidecimal
notation. When I am doing bit twiddling it is easier to visualize the bit
pattern what using hexidecimal notation. One digit in hexidecimal notation
is exactly 4 bits.

The harder question is why use (1<<12) rather than 4096. If they used
0x1000 it would be more obvious. I'd guess that 99% of the time this is
just more readable. Another possible option is that someone was tuning the
code and found a 'trick' for a particular processor/compiler.

It is not impossible that a compiler will take:

x = (1<<12);

and optimize it to a STORE IMMEDIATE 1 and SHIFT opcode. The cycle count
for this might be faster than trying to assign 4096. This is a stretch but
not impossible. Mind you, I'd put a comment next to the code in case a
revision in the compiler or a port to a different architecture made this
tweek disappear.

--
Send e-mail to: darrell at cs dot toronto dot edu
Don't send e-mail to vi************@ whitehouse.gov
Nov 14 '05 #7

"bollod" <bo****@lycos.c om> wrote in message
news:pa******** *************** *****@lycos.com ...
On Fri, 26 Dec 2003 04:03:30 +0000, pete wrote:
bollod wrote:

Is there any benefit to declaring a variable:

x = (1<<12);

instead of:

x = 4096;

(besides the geek appeal of course ;-) )

I have seen these kind of declarations come up
in some code I have been
fiddling with and was wondering if there was any tangible benefit,
(I have also seen stuff declared as hex,
is there any benefit to that either).


It's easier to visualise the binary representation
of the values that way,
which is especially good if your numbers are masks.


That makes sense.

What would you do if you needed to create the mask '10101010' (besides
foo=170 or foo=0xA4)?

---
bollod


x=2<<6|2<<4|2<< 2|2;

or

x=10<<4|10;

or

x=5<<5|5<<1;

or

x=85<<1;
But now its just getting silly.....

Sean
Nov 14 '05 #8
bollod wrote:
On Fri, 26 Dec 2003 04:03:30 +0000, pete wrote:
It's easier to visualise the binary representation
of the values that way,
which is especially good if your numbers are masks.


That makes sense.

What would you do if you needed to create the mask '10101010' (besides
foo=170 or foo=0xA4)?


#define l )*2+1
#define O )*2
#define b1 ((((((((0

unsigned char foo = b1 l O l O l O l O ;

--
Richard Heathfield : bi****@eton.pow ernet.co.uk
"Usenet is a strange place." - Dennis M Ritchie, 29 July 1999.
C FAQ: http://www.eskimo.com/~scs/C-faq/top.html
K&R answers, C books, etc: http://users.powernet.co.uk/eton
Nov 14 '05 #9
bollod wrote:
Is there any benefit to declaring a variable:

x = (1<<12);

instead of:

x = 4096;

(besides the geek appeal of course ;-) ) If x is used as a "common variable" I can see no benefit.

But if x is used as some kind of bit-mask you got the benefit of
immediately seeing that the value of x is 1000000000000 in binary notation.

As (1<<n) can be read as 1 followed by n zero digits in binary notation.
For example: 1<<3 is 1000 in binary.
(I have also seen stuff declared as hex, is there any benefit to that either).


Hexadecimal notation is also very common when using bit-masks.
As Darrell Grainger already said:
| One digit in hexidecimal notation
| is exactly 4 bits.

So if you familiar with hexadecimal notation it's easy to see that
0x1000 (4096) is 1000000000000.

I also have seen code with bit-masks in octal notation (one digit in
octal notation represents 3 bits).

HTH,
Robert

--
Ro************* @rbdev.net
Nov 14 '05 #10

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

Similar topics

27
1971
by: ajay | last post by:
Why would a new of object be created without assigning it to any of variable? new A; ??? tx
15
3210
by: lawrence | last post by:
Sorry for the dumb question but I'm new to Javascript. I wrote this script hoping to animate some div blocks on a page. You can see the page here: http://www.keymedia.biz/demo.htm Can anyone tell me why these DIVs don't drift to the left as they are supposed to? <script language="javascript">
6
10115
by: adamrfrench | last post by:
Let it be mentioned that Javascript is not my forte, so the solution to this could very well be a simple one. I am working on an AJAX function where I can pass a URL and the target ID in, and have the function update the target ID with the URL. There is a bit more to it then that, but that is the basics. my difficulty comes when I try to...
18
4326
by: Jen | last post by:
I'm using Microsoft's own VB.NET FTP Example: http://support.microsoft.com/default.aspx?scid=kb;en-us;832679 I can get the program to create directories, change directories, etc., but I can't get it to upload a file to the FTP server. I just get a "Cannot connect to remote server" error after this TRY: s = New...
8
4892
by: Eric.Medlin | last post by:
I am reading in 1bit data to a buffer that contains over 30000 bits and I would like to beable to bitshift the entire buffer and AND and OR it with other buffers of the same size. I though I could use the stl bitset class, but I cannot find a way to load the buffer into the bitset. I have been trying with this test program (with the size...
1
5722
by: garry.oxnard | last post by:
Can anyone help me to solve a problem which involves switching from Access to Excel (then back to Access) programatically please? I have an Excel template which, on open, also opens an Access database - containing a list of addresses. Sequence of events is = (1) Excel template opens in its default XXX.xls filename. (2) Code runs to save...
25
2554
by: samjnaa | last post by:
Please check for sanity and approve for posting at python-dev. In Visual Basic there is the keyword "with" which allows an object- name to be declared as governing the following statements. For example: with quitCommandButton .enabled = true .default = true end with
6
3005
by: nanodust | last post by:
hello all i am relatively new to python, catching on, but getting stuck on simple thing: i have two string bytes i need to push into a single (short) int, like so in c: temp = strBuf;
10
12430
by: dan | last post by:
Am i breaking any rules when I loop dates like // Determine Memorial Day intFlag = 0; memDayHol = new Date (currentYear, 4, 31); while (intFlag == 0) { if (memDayHol.getDay() == 1) {intFlag =1;} else {memDayHol = memDayHol - 1;} }
0
7584
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language...
0
7888
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. ...
0
8108
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that...
1
7644
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For...
0
7951
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the...
0
6260
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then...
0
3643
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in...
0
3626
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
925
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating...

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.