bitwise shift? | Bytes (2024)

desktop

I have found a code example with this loop.

for k in range(10, 25):
n = 1 << k;
I have never read Python before but is it correct that 1 get multiplied
with the numbers 10,11,12,12,... ,25 assuming that 1 << k means "1 shift
left by k" which is the same as multiplying with k.

Apr 25 '07 #1

Subscribe Reply

10 bitwise shift? | Bytes (1) 2574 bitwise shift? | Bytes (2)

  • 1
  • 2
  • >

Michael Hoffman

desktop wrote:

I have found a code example with this loop.

for k in range(10, 25):
n = 1 << k;
I have never read Python before but is it correct that 1 get multiplied
with the numbers 10,11,12,12,... ,25

No.

assuming that 1 << k means "1 shift left by k"

Yes.

which is the same as multiplying with k.

No.

Try starting the Python interpreter and entering 1 << 10.
--
Michael Hoffman

Apr 25 '07 #2

=?ISO-8859-1?Q?Thomas_Kr=FCger?=

desktop schrieb:

I have found a code example with this loop.

for k in range(10, 25):
n = 1 << k;
I have never read Python before but is it correct that 1 get multiplied
with the numbers 10,11,12,12,... ,25 assuming that 1 << k means "1 shift
left by k" which is the same as multiplying with k.

1 << n
is a more efficient replacement for
2**n

Thomas

--
sinature: http://nospam.nowire.org/signature_usenet.png

Apr 25 '07 #3

Sherm Pendley

desktop <ff*@sss.comwri tes:

for k in range(10, 25):
n = 1 << k;

I have never read Python before but is it correct that 1 get
multiplied with the numbers 10,11,12,12,... ,25 assuming that 1 << k
means "1 shift left by k" which is the same as multiplying with k.

Shift left is *not* the same as multiplying by k. It is the same as multi-
plying by 2^k. That is, 1<<10 = 1024, 1<<11 = 2048, 1<<12 = 4096, etc.

sherm--

--
Web Hosting by West Virginians, for West Virginians: http://wv-www.net
Cocoa programming in Perl: http://camelbones.sourceforge.net

Apr 25 '07 #4

Terry Reedy

"desktop" <ff*@sss.comwro te in message
news:f0******** **@news.net.uni-c.dk...
|I have found a code example with this loop.
|
| for k in range(10, 25):
| n = 1 << k;
|
|
| I have never read Python before but is it correct ...

One of the super-nice feature of Python is the interactive mode, also
available with IDLE and other IDEs. that lets you explore the meaning of
Python code faster than you can ask here. Use it and learn.

>>for k in range(10, 25): print 1 << k

1024
2048
4096
8192
16384
32768
65536
131072
262144
524288
1048576
2097152
4194304
8388608
16777216

Apr 25 '07 #5

John Machin

On 26/04/2007 7:10 AM, Sherm Pendley wrote:

Shift left is *not* the same as multiplying by k. It is the same as multi-
plying by 2^k.

Where I come from, ^ is the exclusive-or operator. Of course YMMV in WV :-)

Apr 26 '07 #6

Sherm Pendley

John Machin <sj******@lexic on.netwrites:

On 26/04/2007 7:10 AM, Sherm Pendley wrote:
>Shift left is *not* the same as multiplying by k. It is the same as multi-
plying by 2^k.

Where I come from, ^ is the exclusive-or operator. Of course YMMV in WV :-)

Make that YMMV in VB. :-(

Funny thing is, I haven't written in VB since '95 or thereabouts. Let that be
a lesson for the youngsters - touch not the unclean beast called VB, lest its
vileness stain thy code for all time!

sherm--

--
Web Hosting by West Virginians, for West Virginians: http://wv-www.net
Cocoa programming in Perl: http://camelbones.sourceforge.net

Apr 26 '07 #7

Tobiah

John Machin wrote:

On 26/04/2007 7:10 AM, Sherm Pendley wrote:
>Shift left is *not* the same as multiplying by k. It is the same as
multi-
plying by 2^k.

Where I come from, ^ is the exclusive-or operator. Of course YMMV in WV :-)

desktops:toby:g abc
bc 1.06
Copyright 1991-1994, 1997, 1998, 2000 Free Software Foundation, Inc.
This is free software with ABSOLUTELY NO WARRANTY.
For details type `warranty'.
2^3
8

--
Posted via a free Usenet account from http://www.teranews.com

May 2 '07 #8

Tobiah

Tobiah wrote:

John Machin wrote:
>On 26/04/2007 7:10 AM, Sherm Pendley wrote:
>>Shift left is *not* the same as multiplying by k. It is the same as
multi-
plying by 2^k.

Where I come from, ^ is the exclusive-or operator. Of course YMMV in
WV :-)

desktops:toby:g abc
bc 1.06
Copyright 1991-1994, 1997, 1998, 2000 Free Software Foundation, Inc.
This is free software with ABSOLUTELY NO WARRANTY.
For details type `warranty'.
2^3
8

Wow, thunderbird displayed this to me as a true exponent, even
though it is an ascii message. anyone else get this?
--
Posted via a free Usenet account from http://www.teranews.com

May 2 '07 #9

mensanator

On May 2, 2:24 pm, Tobiah <t...@tobiah.or gwrote:

John Machin wrote:
On 26/04/2007 7:10 AM, Sherm Pendley wrote:
Shift left is *not* the same as multiplying by k. It is the same as
multi-
plying by 2^k.
Where I come from, ^ is the exclusive-or operator. Of course YMMV in WV :-)

desktops:toby:g abc
bc 1.06
Copyright 1991-1994, 1997, 1998, 2000 Free Software Foundation, Inc.
This is free software with ABSOLUTELY NO WARRANTY.
For details type `warranty'.
2^3
8

OTOH,

IDLE 1.2c1

>>2^3

1

Guess which one is relevant to comp.lang.pytho n?

>
--
Posted via a free Usenet account fromhttp://www.teranews.co m

May 2 '07 #10

  • 1
  • 2
  • >

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

Similar topics

11 9026

Why bitwise operators?

by: Randell D. |last post by:

Why would one use bitwise operators? I can program in various languages in some shape or form (C++, PHP, some scripting) and I've heard/seen bitwise operators before, but never understood why anyone would use them - any real world examples or ideas? Examples follow (that I am reading in my Core JavaScript Guide 1.5). 15 & 9 yields 9 (1111 & 1001 = 1001) 15 | 9 yields 15 (1111 | 1001 = 1111) 15 ^ 9 yields 6 (1111 ^ 1001 = 0110) in...

Javascript

3 5807

mulitiplication using bitwise operators

by: sandy_pt_in |last post by:

Hi C guru's can you please tell me how to do multiplication of 2 numbers using bitwize operators?? expecting reply.

C / C++

8 6939

Checking a bitwise enumeration

by: Paul E Collins |last post by:

Suppose I have a few Keys objects: Keys k1 = Keys.V; // V Keys k2 = Keys.Control | Keys.V; // Ctrl+V Keys k3 = Keys.Shift | Keys.J; // Shift+J I need to determine which of these include the Keys.V element, regardless of any other keys. I know it will be a bitwise comparison, but I can't work out the correct syntax to use.

C# / C Sharp

8 8415

Bitwise shift in VB.Net 2002

by: Rudolf |last post by:

How do you do a bit shift in VB.Net 2002? In VB.Net 2003n you can use the << or >> operators. Thanks Rudolf

Visual Basic .NET

4 5167

A problem about type cast in bitwise shift

by: kernelxu |last post by:

Hi,folks. I got some suggestion about bitwise shift from <The C Book, second edition>(written by Mike Banahan, Declan Brady and Mark Doran, originally published by Addison Wesley in 1991. This version is made freely available at http://publications.gbdirect.co.uk/c_book/) ....snip... The position is clearer if an unsigned operand is right shifted, because there is no choice: it must be a logical shift. For that reason, whenever right...

C / C++

5 5796

Bitwise Operators: Aplication

by: noridotjabi |last post by:

I'm learning to program in C and any tutorial or book that I read likes to briefly touch on birdies operators and then move on without giving any sort of example application of them. Call me what you will but I cannot seem to see the purpose for bitwise operators. Especially the operators bitwise OR ( | ) and bitwise AND ( & ), I'm just not getting it. I have searched around and really haven't found anything that gave explanation to why...

C / C++

1 6669

Rotational bitwise shift

by: rabidusv |last post by:

As I understand it, the unsigned long type is 4 bytes. Also, the bitwise shift (<<) is a zero-fill shift as opposed to a rotational shift. However when I shift, the value seems to be rotating, as opposed to being lost as I would expect. Anybody have any ideas? char resBuffer; unsigned long value=1;

C / C++

5 3309

Bitwise expression

by: Gigs_ |last post by:

Can someone explain me bitwise expression? few examples for every expression will be nice x << y Left shift x >y Right shift x & y Bitwise AND x | y Bitwise OR x ^ y Bitwise XOR (exclusive OR) ~x Bitwise negation

Python

29 5949

Getting rid of bitwise operators in Python 3?

by: Carl Banks |last post by:

Anyone with me here? (I know the deadline for P3 PEPs has passed; this is just talk.) Not many people are bit-fiddling these days. One of the main uses of bit fields is flags, but that's not often done in Python because of keyword arguments and dicts, which are lot more versatile. Another major use, talking to hardware, is not something oft done in Python either. It seems like this occasional usage wouldn't justify having built-in...

Python

5 6817

bitwise operator and endianness

by: Rahul |last post by:

Hi Everyone, I have a program unit which does >and << of an integer which is of 4 bytes length. The logic of shifting and action based on the result, assumes that the system is big-endian. Accordingly, if i need the program to work fine in a little-endian system. I understand that the code needs to be changed. ( I couldn't find any statement in C90 about endianness, hence i'm assuming that c programs are not portable if the endianness...

C / C++

8968

What is ONU?

by: marktang |last post by:

ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...

General

9473

Problem With Comparison Operator <=> in G++

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. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...

C / C++

9334

Maximizing Business Potential: The Nexus of Website Design and Digital Marketing

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 captivates audiences and drives business growth. The Art of Business Website Design Your website is...

Online Marketing

1 9259

The easy way to turn off automatic updates for Windows 10/11

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 most users, this new feature is actually very convenient. If you want to control the update process,...

Windows Server

9208

Discussion: How does Zigbee compare with other wireless protocols in smart home applications?

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 choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...

General

8208

AI Job Threat for Devs

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 launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...

Career Advice

1 6750

Access Europe - Using VBA to create a class based on a table - Wed 1 May

by: isladogs |last post by:

The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...

Microsoft Access / VBA

6053

Couldn’t get equations in html when convert word .docx file to html file in C#.

by: conductexam |last post by:

I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...

C# / C Sharp

4824

Windows Forms - .Net 8.0

by: adsilva |last post by:

A Windows Forms form does not have the event Unload, like VB6. What one acts like?

Visual Basic .NET

bitwise shift? | Bytes (2024)
Top Articles
Latest Posts
Article information

Author: Fredrick Kertzmann

Last Updated:

Views: 5796

Rating: 4.6 / 5 (46 voted)

Reviews: 85% of readers found this page helpful

Author information

Name: Fredrick Kertzmann

Birthday: 2000-04-29

Address: Apt. 203 613 Huels Gateway, Ralphtown, LA 40204

Phone: +2135150832870

Job: Regional Design Producer

Hobby: Nordic skating, Lacemaking, Mountain biking, Rowing, Gardening, Water sports, role-playing games

Introduction: My name is Fredrick Kertzmann, I am a gleaming, encouraging, inexpensive, thankful, tender, quaint, precious person who loves writing and wants to share my knowledge and understanding with you.