Advertising (This ad goes away for registered users. You can Login or Register)

C/C++ How to get least significant bit

Programming on your favorite platform, for your favorite platform? Post here
KAZZQE
Posts: 36
Joined: Mon Feb 14, 2011 1:37 pm

C/C++ How to get least significant bit

Post by KAZZQE » Thu Dec 29, 2011 8:21 pm

Good day.

I need to get the least significant bit of an unsigned char in C/C++.
can you give me an idea, how to solve this?
a C solution is what i prefer, but a C++ solution is also good.(:
Advertising
kazzqe alias tukkitukki
looking don't costs: http://gercode.blogspot.com

the reason for this bad english is that i'm german.. :b

User avatar
codestation
Big Beholder
Posts: 1660
Joined: Wed Jan 19, 2011 3:45 pm
Location: /dev/negi

Re: C/C++ How to get least significant bit

Post by codestation » Thu Dec 29, 2011 8:53 pm

Code: Select all

foo & 1
Advertising
Plugin list
Working on: QPSNProxy, QCMA - Open source content manager for the PS Vita
Playing: Error: ENOTIME
Repositories: github, google code
Just feel the code..

KAZZQE
Posts: 36
Joined: Mon Feb 14, 2011 1:37 pm

Re: C/C++ How to get least significant bit

Post by KAZZQE » Thu Dec 29, 2011 11:09 pm

thx.(:
you're the best.

and how about the most significant bit?
kazzqe alias tukkitukki
looking don't costs: http://gercode.blogspot.com

the reason for this bad english is that i'm german.. :b

Salmon
Posts: 68
Joined: Fri Jan 07, 2011 12:08 pm

Re: C/C++ How to get least significant bit

Post by Salmon » Fri Dec 30, 2011 2:11 am

Code: Select all

(i >> sizeof(i)) & 1
There is probably a better way to do it.

User avatar
codestation
Big Beholder
Posts: 1660
Joined: Wed Jan 19, 2011 3:45 pm
Location: /dev/negi

Re: C/C++ How to get least significant bit

Post by codestation » Fri Dec 30, 2011 4:15 am

Salmon wrote:

Code: Select all

(i >> sizeof(i)) & 1
There is probably a better way to do it.
Not exactly as sizeof of an [unsigned] char is always 1 so you are shifting only one bit.

A correct answer would be:

Code: Select all

foo >> sizeof(foo) * CHAR_BIT - 1 & 1;
Plugin list
Working on: QPSNProxy, QCMA - Open source content manager for the PS Vita
Playing: Error: ENOTIME
Repositories: github, google code
Just feel the code..

Salmon
Posts: 68
Joined: Fri Jan 07, 2011 12:08 pm

Re: C/C++ How to get least significant bit

Post by Salmon » Fri Dec 30, 2011 4:55 am

Woops. Thanks for picking up on that mistake.

KAZZQE
Posts: 36
Joined: Mon Feb 14, 2011 1:37 pm

Re: C/C++ How to get least significant bit

Post by KAZZQE » Fri Dec 30, 2011 11:51 am

ok. thanks for your answers.(:

i used the code of @codestation, but made it shorter..
would it have the same effect?

Code: Select all

msb = (foo >> 7) & 1;
kazzqe alias tukkitukki
looking don't costs: http://gercode.blogspot.com

the reason for this bad english is that i'm german.. :b

User avatar
m0skit0
Guru
Posts: 3817
Joined: Mon Sep 27, 2010 6:01 pm

Re: C/C++ How to get least significant bit

Post by m0skit0 » Fri Dec 30, 2011 12:03 pm

Nope because you're not taking into account the size of foo (that's why sizeof() is used...). Using sizeof() is way better because foo can be any size. Using 7 is less portable. Also using literals is a bad programming practice.

Keep in mind that sizeof() (IIRC) is a compile-time function, and thus you don't have any speed penalty using it.

Resuming: always use sizeof()
I wanna lots of mov al,0xb
Image
"just not into this RA stuffz"

KAZZQE
Posts: 36
Joined: Mon Feb 14, 2011 1:37 pm

Re: C/C++ How to get least significant bit

Post by KAZZQE » Sun Jan 01, 2012 3:22 pm

but..
..sizeof(an unsigned char) is 1.
..CHAR_BIT is 8.

1 * 8 - 1 is 7?

the unsigned char cannot change its own type and CHAR_BIT has a constant value..
or did i understand wrong..?
kazzqe alias tukkitukki
looking don't costs: http://gercode.blogspot.com

the reason for this bad english is that i'm german.. :b

User avatar
codestation
Big Beholder
Posts: 1660
Joined: Wed Jan 19, 2011 3:45 pm
Location: /dev/negi

Re: C/C++ How to get least significant bit

Post by codestation » Sun Jan 01, 2012 4:05 pm

KAZZQE wrote:but..
..sizeof(an unsigned char) is 1.
In C99 this is guaranteed.
KAZZQE wrote:..CHAR_BIT is 8.
Nope, you can find DSP where CHAR_BIT is 16 or even 32.

I used sizeof on my example (even with a char variable), because it can be used without modifications for other data types like short and int without guessing the the size value. For example you can find cases where sizeof(int) == 1 :?. Like m0skit0 said already, try to avoid literals on your code so it can be more portable.
Plugin list
Working on: QPSNProxy, QCMA - Open source content manager for the PS Vita
Playing: Error: ENOTIME
Repositories: github, google code
Just feel the code..

Post Reply

Return to “Programming”