Author Topic: Getting the bits out of a number  (Read 3975 times)

0 Members and 1 Guest are viewing this topic.

Offline Spyro543

  • LV9 Veteran (Next: 1337)
  • *********
  • Posts: 1189
  • Rating: +74/-3
    • View Profile
Getting the bits out of a number
« on: January 18, 2013, 02:21:54 pm »
Let's say I have a variable that is char type and is equal to 'A' which is 65 in decimal and 01000001 in binary. How can I access the individual bits in that variable? For example, accessing bit 0 would result in 1. How can I do this in C?

Offline Juju

  • Incredibly sexy mare
  • Coder Of Tomorrow
  • LV13 Extreme Addict (Next: 9001)
  • *************
  • Posts: 5730
  • Rating: +500/-19
  • Weird programmer
    • View Profile
    • juju2143's shed
Re: Getting the bits out of a number
« Reply #1 on: January 18, 2013, 02:26:46 pm »
You would want to use the & operator. Say char = 65 and you want bit 6 , 26 = 64, so you would do something like char & 64 == 64. If it returns true, the bit is set, false, the bit isn't set.
« Last Edit: January 18, 2013, 02:28:04 pm by Juju »

Remember the day the walrus started to fly...

I finally cleared my sig after 4 years you're happy now?
THEGAME
This signature is ridiculously large you've been warned.

The cute mare that used to be in my avatar is Yuki Kagayaki, you can follow her on Facebook and Tumblr.

Offline ruler501

  • Meep
  • LV11 Super Veteran (Next: 3000)
  • ***********
  • Posts: 2475
  • Rating: +66/-9
  • Crazy Programmer
    • View Profile
Re: Getting the bits out of a number
« Reply #2 on: January 18, 2013, 02:35:48 pm »
I would do it like this in C++
Code: [Select]
template <class T>
bool retrievebit(T source, int bit)
{
if(bit > (sizeof(T)*8)) throw "Too large";
return ((source >> (bit-1)) & 1);
}
I haven't tested that and it will only work for types that have >> defined as the left shift, but  otherwise it should be good.

EDIT: I just saw this was in C I'll rewrite this real quick to be C code

In c for just char it would be like this
Code: [Select]
int retrievebit(char source, int bit)
{
if(bit > 8)) return -1;
return ((source >> (bit-1)) & 1);
}
« Last Edit: January 18, 2013, 02:37:36 pm by ruler501 »
I currently don't do much, but I am a developer for a game you should totally try out called AssaultCube Reloaded download here https://assaultcuber.codeplex.com/
-----BEGIN GEEK CODE BLOCK-----
Version: 3.1
GCM/CS/M/S d- s++: a---- C++ UL++ P+ L++ E---- W++ N o? K- w-- o? !M V?
PS+ PE+ Y+ PGP++ t 5? X R tv-- b+++ DI+ D+ G++ e- h! !r y

Offline Juju

  • Incredibly sexy mare
  • Coder Of Tomorrow
  • LV13 Extreme Addict (Next: 9001)
  • *************
  • Posts: 5730
  • Rating: +500/-19
  • Weird programmer
    • View Profile
    • juju2143's shed
Re: Getting the bits out of a number
« Reply #3 on: January 18, 2013, 02:37:50 pm »
Actually, this could work:

Code: [Select]
#define getBit(x,n) x&(1<<n)

Remember the day the walrus started to fly...

I finally cleared my sig after 4 years you're happy now?
THEGAME
This signature is ridiculously large you've been warned.

The cute mare that used to be in my avatar is Yuki Kagayaki, you can follow her on Facebook and Tumblr.

Offline Vogtinator

  • LV9 Veteran (Next: 1337)
  • *********
  • Posts: 1193
  • Rating: +108/-5
  • Instruction counter
    • View Profile
Re: Getting the bits out of a number
« Reply #4 on: January 18, 2013, 02:47:29 pm »
Quote
Actually, this could work:
Code: [Select]
#define getBit(x,n) x&(1<<n)
Nope :P
Code: [Select]
#define getBit(x,n) ((x&(1<<n))>>n)

Offline Juju

  • Incredibly sexy mare
  • Coder Of Tomorrow
  • LV13 Extreme Addict (Next: 9001)
  • *************
  • Posts: 5730
  • Rating: +500/-19
  • Weird programmer
    • View Profile
    • juju2143's shed
Re: Getting the bits out of a number
« Reply #5 on: January 18, 2013, 02:50:18 pm »
Quote
Actually, this could work:
Code: [Select]
#define getBit(x,n) x&(1<<n)
Nope :P
Code: [Select]
#define getBit(x,n) ((x&(1<<n))>>n)
Oh right, I forgot making it return 1 or 0 and not (1<<n) or 0.

Remember the day the walrus started to fly...

I finally cleared my sig after 4 years you're happy now?
THEGAME
This signature is ridiculously large you've been warned.

The cute mare that used to be in my avatar is Yuki Kagayaki, you can follow her on Facebook and Tumblr.

Offline calc84maniac

  • eZ80 Guru
  • Coder Of Tomorrow
  • LV11 Super Veteran (Next: 3000)
  • ***********
  • Posts: 2912
  • Rating: +471/-17
    • View Profile
    • TI-Boy CE
Re: Getting the bits out of a number
« Reply #6 on: January 18, 2013, 02:51:46 pm »
Or even
Code: [Select]
#define getBit(x,n) ((x)>>(n)&1)
"Most people ask, 'What does a thing do?' Hackers ask, 'What can I make it do?'" - Pablos Holman