[LinuxBIOS] Another dumb C question

Hello, I have another newbie C question. Is this the same as 2^9 ?? value = 9 (2 << (value - 1)) This will give me 512, but will it work no matter what "value" is? Or does linuxbios already have a "to the power of" global function? Thanks - Joe

On Thu, Jul 12, 2007 at 05:00:23AM -0400, Joseph Smith wrote:
Hello, I have another newbie C question. Is this the same as 2^9 ??
Note that ^ means XOR in C. Exclusive or. Output 1 when only one input bit is 1, but not both. 00100011 ^ 10101010 ---------- 10001001
value = 9
(2 << (value - 1))
This will give me 512, but will it work no matter what "value" is?
Yep.
Or does linuxbios already have a "to the power of" global function?
No pow() in LB and the pow() in libm uses doubles so it's bad for the task. Bit shift is fine methinks but I would suggest: (1 << value) instead of (2 << (value - 1)) //Peter

Quoting Peter Stuge <peter@stuge.se>:
value = 9
(2 << (value - 1))
Bit shift is fine methinks but I would suggest:
(1 << value)
instead of
(2 << (value - 1))
//Peter
Not sure I understand. I am trying to get 2 "to the power of" value. if value = 9 (1 << value) This equals 18???? Not 2^9 = 512 where (2 << (value - 1)) (2 << (9 - 1)) 2 << 8 = 512 Thanks - Joe

Joseph Smith wrote:
Quoting Peter Stuge <peter@stuge.se>:
value = 9
(2 << (value - 1))
Bit shift is fine methinks but I would suggest:
(1 << value)
instead of
(2 << (value - 1))
//Peter
Not sure I understand. I am trying to get 2 "to the power of" value.
if value = 9 (1 << value) This equals 18???? Not 2^9 = 512 where (2 << (value - 1)) (2 << (9 - 1)) 2 << 8 = 512
Thanks - Joe
remember that 2 = (1 << 1). so 2 << (value - 1) = 1 << value. I'm not sure where you're getting an 18 from, that makes no sense at all (since 18 in either hex or dec has two non-zero bits, so it's impossible to get that result with just that binary shift). -Corey

Quoting Corey Osgood <corey.osgood@gmail.com>:
Joseph Smith wrote:
Quoting Peter Stuge <peter@stuge.se>:
value = 9
(2 << (value - 1))
Bit shift is fine methinks but I would suggest:
(1 << value)
instead of
(2 << (value - 1))
//Peter
Not sure I understand. I am trying to get 2 "to the power of" value.
if value = 9 (1 << value) This equals 18???? Not 2^9 = 512 where (2 << (value - 1)) (2 << (9 - 1)) 2 << 8 = 512
Thanks - Joe
remember that 2 = (1 << 1). so 2 << (value - 1) = 1 << value. I'm not sure where you're getting an 18 from, that makes no sense at all (since 18 in either hex or dec has two non-zero bits, so it's impossible to get that result with just that binary shift).
-Corey
Well lets see if value = 9, which is 1001 in binary. If you bitshift 1001 one position to the left it becomes 10010, correct? Which is 18 in decimal. Thanks - Joe

Quoting Joseph Smith <joe@smittys.pointclark.net>:
Quoting Corey Osgood <corey.osgood@gmail.com>:
Joseph Smith wrote:
Quoting Peter Stuge <peter@stuge.se>:
value = 9
(2 << (value - 1))
Bit shift is fine methinks but I would suggest:
(1 << value)
instead of
(2 << (value - 1))
//Peter
Not sure I understand. I am trying to get 2 "to the power of" value.
if value = 9 (1 << value) This equals 18???? Not 2^9 = 512 where (2 << (value - 1)) (2 << (9 - 1)) 2 << 8 = 512
Thanks - Joe
remember that 2 = (1 << 1). so 2 << (value - 1) = 1 << value. I'm not sure where you're getting an 18 from, that makes no sense at all (since 18 in either hex or dec has two non-zero bits, so it's impossible to get that result with just that binary shift).
-Corey
Well lets see if value = 9, which is 1001 in binary. If you bitshift 1001 one position to the left it becomes 10010, correct? Which is 18 in decimal.
Thanks - Joe
Nevermind I get it. I was looking at it bass ackwards :-) Sorry, I'm a little tired. Thanks - Joe
participants (3)
-
Corey Osgood
-
Joseph Smith
-
Peter Stuge