473,320 Members | 1,887 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,320 software developers and data experts.

shift question

Hi there!

I have a very silly shift operation question:

My test code does something like this:
int j=1;
int y=0;
int i=0;
int bs=0;

for(i=0;i<16;i++){
bs=1<<j;
y=?????;
printf("%i:\tj=%i\tbs=%i\t\ty=%i\n", i, j, bs, y);
j++;
}

I want y to be the bit set in bs=1<<j - I know usally done by y=j...

But in my real application I have a bs=1024 or bs=512 and have to
determine the y...

Thanx a lot!
Johann

Apr 16 '06 #1
3 1788
Ico
horvatj <ho***********@gmx.net> wrote:

My test code does something like this:
int j=1;
int y=0;
int i=0;
int bs=0;

for(i=0;i<16;i++){
bs=1<<j;
y=?????;
printf("%i:\tj=%i\tbs=%i\t\ty=%i\n", i, j, bs, y);
j++;
}

I want y to be the bit set in bs=1<<j - I know usally done by y=j...

But in my real application I have a bs=1024 or bs=512 and have to
determine the y...


Your question is not very clear, but I assume that what you are looking
for is the base-2 logarithm of a number. If performance is not a big
issue, you can use the log() function from your local math library :

#include <math.h>

...

int x = 512;
int y;

y = log(x) / log(2);

Another method would be to look for the first bit that is set to 1 with
a simple loop (assuming 16 bit integers, like in your example) :

int x = 512;
int y;

for(y=15; y>=0; y--) if(x & (1<<y)) break;

or something like :

int x = 512;
int y = 16;
int mask = 1<<16;

while(!(x & mask)) {
mask >>= 1;
y --;
}

This can be further optimized by using binary search methods. If you are
really interested, you can find more recipes in the book "Hacker's
Delight", chapter 5-3.
--
:wq
^X^Cy^K^X^C^C^C^C
Apr 16 '06 #2
"Ico" <us****@zevv.nl> wrote in message
news:44**********************@dreader30.news.xs4al l.nl...
horvatj <ho***********@gmx.net> wrote:

My test code does something like this:
int j=1;
int y=0;
int i=0;
int bs=0;

for(i=0;i<16;i++){
bs=1<<j;
y=?????;
printf("%i:\tj=%i\tbs=%i\t\ty=%i\n", i, j, bs, y);
j++;
}

I want y to be the bit set in bs=1<<j - I know usally done by y=j...

But in my real application I have a bs=1024 or bs=512 and have to
determine the y...
Your question is not very clear, but I assume that what you are looking
for is the base-2 logarithm of a number. If performance is not a big
issue, you can use the log() function from your local math library :

#include <math.h>

...

int x = 512;
int y;

y = log(x) / log(2);

Another method would be to look for the first bit that is set to 1 with
a simple loop (assuming 16 bit integers, like in your example) :

int x = 512;
int y;

for(y=15; y>=0; y--) if(x & (1<<y)) break;

or something like :

int x = 512;
int y = 16;
int mask = 1<<16;


Should not this be 15 instead of 16?
while(!(x & mask)) {
mask >>= 1;
y --;
}

Apr 16 '06 #3
Ico
stathis gotsis <st***********@hotmail.com> wrote:
"Ico" <us****@zevv.nl> wrote in message
Your question is not very clear, but I assume that what you are looking
for is the base-2 logarithm of a number. If performance is not a big
issue, you can use the log() function from your local math library :

#include <math.h>

...

int x = 512;
int y;

y = log(x) / log(2);

Another method would be to look for the first bit that is set to 1 with
a simple loop (assuming 16 bit integers, like in your example) :

int x = 512;
int y;

for(y=15; y>=0; y--) if(x & (1<<y)) break;

or something like :

int x = 512;
int y = 16;
int mask = 1<<16;


Should not this be 15 instead of 16?


Indeed, it should.

--
:wq
^X^Cy^K^X^C^C^C^C
Apr 16 '06 #4

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

Similar topics

3
by: Antioch | last post by:
Ok, so Im a newb python programmer and I'm trying to create a simple python web-application. The program is simply going to read in pairs of words, parse them into a dictionary file, then randomly...
44
by: Carlos Andr?s | last post by:
Hi everybody. I've got a problem. I'd like to avoid opening a new window when you have pressed the shift key and you click in the left button of the mouse. I've tried the next solution, in the...
43
by: Mehta Shailendrakumar | last post by:
Hello, Can anyone suggest me operator to perform arithmetic shift in C? May it be for a perticular compiler. Thank you in advance. Regards, Shailendra
11
by: Kenneth Lantrip | last post by:
Anyone got any ideas as to how this process could be improved for speed? this is what I have... Dim j, q As Integer Dim x(16), y(16) As Byte x.CopyTo(y, 0) ' shift left circular 24 bits
13
by: HARDCORECODER | last post by:
ok here is the question. I want to exract the first 4 bits in a int so let say int b = somenumber; int result= 0; result = b << 4; if I got this right result should contain the 4 bits that...
1
by: jonathanmcdougall | last post by:
I am developing a calendar on which dates can be clicked and selected. A calendar is a table in which each day is a cell. Each cell has a unique id. By left-clicking on a date, it gets selected...
7
by: gokkog | last post by:
Hello, Recently I have the book Programming Pearls. Nice read! Perhaps it is weekend, I cannot understand the following codes well: #define BITSPERWORD 32 #define SHIFT 5 #define MASK 0x1F...
3
by: Amy Smith | last post by:
Hello there, I am having a small problem which been challenging me for few days and need help or advice. I am trying to calculate the day-shift for employees based on the time they started and...
6
by: saumya.agarwal | last post by:
Hi, I am using libxml2 for xml parsing. When the client application sends data to libxml2 in UTF-8 format, it works fine. But, I have a scenarion in which the client application sends data to...
4
by: hui11 | last post by:
Hi, According to the doc at mozilla, http://developer.mozilla.org/en/docs/DOM:event.charCode, the charCode takes shift into consideration when pressed. I found that to be true for other cases...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 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 former...

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.