Get a Specific Bit from Byte

Get a specific bit from byte

Easy. Use a bitwise AND to compare your number with the value 2^bitNumber, which can be cheaply calculated by bit-shifting.

//your black magic
var bit = (b & (1 << bitNumber-1)) != 0;

EDIT: To add a little more detail because there are a lot of similar answers with no explanation:

A bitwise AND compares each number, bit-by-bit, using an AND join to produce a number that is the combination of bits where both the first bit and second bit in that place were set. Here's the logic matrix of AND logic in a "nibble" that shows the operation of a bitwise AND:

  0101
& 0011
----
0001 //Only the last bit is set, because only the last bit of both summands were set

In your case, we compare the number you passed with a number that has only the bit you want to look for set. Let's say you're looking for the fourth bit:

  11010010
& 00001000
--------
00000000 //== 0, so the bit is not set

11011010
& 00001000
--------
00001000 //!= 0, so the bit is set

Bit-shifting, to produce the number we want to compare against, is exactly what it sounds like: take the number, represented as a set of bits, and shift those bits left or right by a certain number of places. Because these are binary numbers and so each bit is one greater power-of-two than the one to its right, bit-shifting to the left is equivalent to doubling the number once for each place that is shifted, equivalent to multiplying the number by 2^x. In your example, looking for the fourth bit, we perform:

       1 (2^0) << (4-1) ==        8 (2^3)
00000001 << (4-1) == 00001000

Now you know how it's done, what's going on at the low level, and why it works.

Python: How do I extract specific bits from a byte?

To answer the second part of your question, you can get specific bit values using bitwise operations

# getting your message as int
i = int("140900793d002327", 16)

# getting bit at position 28 (counting from 0 from right)
(i >> 28) & 1

# getting bits at position 24-27
bin((i >> 24) & 0b111)

How to get bit values from Byte?

You can use BitArray() constructor BitArray((Byte[]) to get bits array from byte array. Use indexer on the bit array to get the required bit.

var bits = new BitArray(_byte);

Now bit 16 would be bits[15];

You can follow this example to understand how you can get the require bit.

byte[] _byte = new byte[3] {1,3,7};
var bits = new BitArray(_byte);
for(int i=0; i < bits.Length; i++)
Console.WriteLine(bits[i]);

Output:

True present 1 at bit and false present 0 at bit

First Byte Value = 1 = 00000001 (Binary)

True   = 1            0 bit index 
False = 0 1 bit index
False = 0 2 bit index
False = 0 3 bit index
False = 0 4 bit index
False = 0 5 bit index
False = 0 6 bit index
False = 0 7 bit index

Second Byte Value = 3 = 00000011 (Binary)

True   = 1            8 bit index 
True = 1 9 bit index
False = 0 10 bit index
False = 0 11 bit index
False = 0 12 bit index
False = 0 13 bit index
False = 0 14 bit index
False = 0 15 bit index

Third Byte Value = 7 = 00000111 (Binary)

True   = 1            16 bit index 
True = 1 17 bit index
True = 1 18 bit index
False = 0 19 bit index
False = 0 20 bit index
False = 0 21 bit index
False = 0 22 bit index
False = 0 23 bit index

How to access a specific bit given a byte in C?

This two functions can handle 64 bit value.

uint8_t get_bit(uint64_t bits, uint8_t pos)
{
return (bits >> pos) & 0x01;
}

uint64_t set_bit(uint64_t bits, uint8_t pos, uint8_t value)
{
uint64_t mask = 1LL << (63 - pos);
if (value)
bits |= mask;
else
bits &= ~mask;
return bits;
}

uint64_t v = ...;
uint8_t i = 63;
for(;i>=0; i--)
{
if(get_bit(v, i)){
v=set_bit(v,i, 0);
break;
}
}

C - Get a bit from a byte

Firstoff, 0b prefix is not C but a GCC extension of C.
To get the value of the bit 3 of an uint8_t a, you can use this expression:

((a >> 3)  & 0x01)

which would be evaluated to 1 if bit 3 is set and 0 if bit 3 is not set.

How to get the value of a bit at a certain position from a byte?

public byte getBit(int position)
{
return (ID >> position) & 1;
}

Right shifting ID by position will make bit #position be in the furthest spot to the right in the number. Combining that with the bitwise AND & with 1 will tell you if the bit is set.

position = 2
ID = 5 = 0000 0101 (in binary)
ID >> position = 0000 0001

0000 0001 & 0000 0001( 1 in binary ) = 1, because the furthest right bit is set.

Read 'N' bit from a byte

You must define precisely how you count the bits:

  • starting at 0 or 1
  • from least significant to most significant or the other way?

Assuming bit 0 is the least significant, you can get bit 7 with this expression:

int bit7 = ((unsigned char)Buffer[0] >> 7) & 1;

Here is a generic loop:

for (int i = 7; i >= 0; i--) {
putchar('0' + (((unsigned char)Buffer[0] >> i) & 1));
}

How do I get the bits from a byte in golang?

You can see the visual representation of bits using fmt.Sprintf(%08b, ..) as others have already suggested.

However, if you want to use the bits in operations, such as for calculating the Hamming distance, you'll need to use bitwise operators.

To calculate the nth bit of a byte, you'll need to bitwise AND that byte with another byte whose nth bit is set to 1 and the rest to 0 (aka masking). In other words, that other byte (mask) is the number 2^n-1.

For example, to find the 1st bit of the number 13 (00001101), we would have to mask it with 2^0 = 1 (00000001). We compare the output of performing bitwise AND on both numbers to the mask. If they are equal, it means that the nth bit is 1, otherwise it is 0. We continue like this and find all the bits. Illustrated in Go code:

fmt.Print(13 & 1) // Output: 1 -> 1
fmt.Print(13 & 2) // Output: 2 -> 0
fmt.Print(13 & 4) // Output: 4 -> 1
fmt.Print(13 & 8) // Output: 8 -> 1
// Not necessary to continue, but shown for the sake of the example
fmt.Print(13 & 16) // Output: 0 -> 0
fmt.Print(13 & 32) // Output: 0 -> 0
fmt.Print(13 & 64) // Output: 0 -> 0
fmt.Print(13 & 128) // Output: 0 -> 0

Therefore, 13 in binary is 00001101

Here's a function I wrote recently for calculating the Hamming distance between two arrays of bytes. Just pass an array consisting of a single byte each in your case

func hamming(a, b []byte) (int, error) {
if len(a) != len(b) {
return 0, errors.New("a b are not the same length")
}

diff := 0
for i := 0; i < len(a); i++ {
b1 := a[i]
b2 := b[i]
for j := 0; j < 8; j++ {
mask := byte(1 << uint(j))
if (b1 & mask) != (b2 & mask) {
diff++
}
}
}
return diff, nil
}

Go Playground: https://play.golang.org/p/O1EGdzDYAn

Get values of specific bits in a byte

Old, but it is the top of the search results as the top hit. I don't think it will hurt to explain why in some detail.

Quick and dirty answer after the heading below

To get specific bits, one way to do it is by a "bitmask". A bitmask says that you are only going to let certain bits through. Just like when you mask something to paint.

logical AND is the instruction we want. Logical operations compare individual bits. Its the central operation of anything digital- anything that operates like a computer. When you get all the way down, all any chip is doing is comparing single bits in new and creative ways. There are 2 operations and 2 modifiers giving AND, OR, NOR, XOR, NAND, NOT, XNOR.

I threw a summary on Logical Comparators at the end. Review it if needed.
(in retrospect, I think this is the most valuable part for a newbie.)

But if we set one bit to be what we want, then on the other side well get what we want. Say I want bits 2, 3, 4, and 7. 7- Y N N Y Y Y N N - 0 right. Well, then I create a mask with 1's there. 1 0 0 1 1 1 0 0 and do logical AND.

So then it's just simple math. The computer speaks binary. I just give it the number that 0 0 0 1 1 1 0 0 corresponds to.

Counting in binary. Far right = 1, and then we double in each position. We also start counting at 0. So the 0th bit on the right = 1, the first bit is 2, the 3rd =4, then 8, 16, 32, 64, 128. Play with this if you want, but using this number you can get any value from 0 to 255. (or 127 to -128) but the negatives are more complicated. Might be relevant though.

It's important to note that there are two types of integers in a computer language- Signed, and Unsigned. That last bit- the 7th bit (which is the 8th bit to your or I, is worth 128 an unsigned int, but it's just equal to the sign in the signed int. Surprisingly, they both have the same number of digits (or maybe not surprisingly once you think about it)

As an example, lets take the given number [0 0 0 1 1 1 0 0] and make the first bit a 1 {1 0 0 1 1 1 0 0}, If this is an unsigned integer, its worth 156 (128 + 16+ 8+ 4), but if the 7th bit is a sign bit-- Calculating it is slightly complicated. You must use ("Two's complement") to get the value of 1001110. And while it has a fascinating history that is worth discussing, just not here. The signed bit is calculated by flipping every bit- because in negative numbers- 0 now counts as 1's and 1's are 0's. So lets take {1 0 0 1 1 1 0 0} and make it a format were used to- {0 1 1 0 0 0 1 1}, we just have to remember its negative. The value of it, flipping and add 1: -1 + (64) + (32) + (2) + (1) = 99(-) and then we must remember to subtract -99 - 1 = -100.

We subtract one because 0000 0000 is 0. And if we called 1111 1111 = 0 then we would have 2 values for 0! (incidentally, this is called One's Complement). So, the next number in line- -1!. I am sure there are better two's compliment discussions and its out the scope of this.

So you say,
Value AND 28 = newValue
then you can AND that.

or do a FOR loop

for x in [0,1,2,4,8,16,32,64,-128]
if (x AND value!= 0){
y = log2(abs(x))
print("the bit is a one in position" + y)
} else {
print("the bit is a 0 in position" + y)

}

Or do a for loop using the power aspect to iterate through.

for(i = 1; i < 18, i++){
byte y = 2 ** n
if ((value & ubyte) == 0) { //is 0}

or this java example which is basically the same:

class BitDemo {
public static void main(String[] args) {
int bitmask = 0x000F;
int val = 0x2222;
// prints "2"
System.out.println(val & bitmask);
}
}

Quick Answer is here

I've aimed to answer the question more generally for all programming languages since this is a 4 yr old question. In assembly, you need to get more creative. As you know, since youre asking, every operation has to be discrete. Really forces you to think.

li $t1, 0x020    # get the sixth bit, HEX 20 = DEC 32  in binary = 0b0010 0000
and $t2, $a0, $t1 #t1 gets the bit
slt $t3, $zero, $t2 # compares zero < t2 and if true stores
#1 into t3, so stores the 6th bit in its own register.

use slt, it's a single op instruction, all the other comparators are pseudo instructions, which is less efficient. It's more elegant.

You can also combine the first two instructions with the psuedoinstruction
**andi** $t2, $a0, 0x02 But the compiler will just convert it to the above in the background. I guess save a register.

Another option, if you know the bit you are looking for is to shift until its bit 0.

srl $v0, $t2, 5 # since we went after the 6th bit

I don't like using andi (and immediete, but I can't remember why. I got some weird behavior in the past, but the shortest way to solve the above:

 #your value = $a0
andi $v0, $a0, 0x01 #already in first bit, no more work
andi $v1, $a0, 0x02
slr $v1, $v1, 1 #since we have 1 bit in 2nd bit, just shift one

In your case, you want the last two bits, so we do 1 + 2 (the bit values)= 3. That's where the answer above gets 0x03. 0x tells the assembler its hex bc the assembler only really likes hex. Though it would take decimal 3 if its good (MARS).

---logical comparators review/intro---
I'm a smart dude I am told. But when I started out all these were really confusing to me and hard to remember. That was until I looked at it for a while and realize something.

It's actually really simple. Now I look at it and think, why was that ever hard. It's easier than 1 + 1 in 1st grade. So if you're reading this thinking why is this guy explaining, its because you don't remember the first time you saw it.

Think of it this way, and it's much easier there are only 2 choices, 2 operations to remember and they're super easy to remember- AND and OR (or neither) for logical comparisons. They're not opposites, they're different, and they do what they say. Then there are 2 modifiers. That's it. Understanding AND and OR are in the name, and knowing the adjectives is super easy.

  • NOT the first one we talk about is the easiest. It's a modifier, more than an operator- it only takes one value, so it's not a logical comparator like OR or AND. It just does the opposite. If it's 1, it says 0. If it's 0, then it's 1. Its opposite is nothing. In a circuit, it would be a wire. Nothing changes. How does it work if it only takes 1 value, I think of it as coming after. You do the OR or AND operation, then flip it. So AND and OR:

AND and OR are simple,

  • AND- if both things are 1 (1 AND 1) then the value is one. Nothing else is true here. 1 AND 0 is FALSE. 0 and 0 are FALSE. You only get paid if you show up to work AND do the work. The FALSE and FALSE wouldn't work in that statement.

  • OR says that if either is 1 then the value is one. That's all. Bring me the book back OR pay me $20 and I won't beat you up. You can do one or the other. What if you do both? That's the important bit. Well, you won't get beaten up so the value of 1 OR 1 is TRUE. IF you don't bring the book or bring $20 then 'not getting beat up'= FALSE.

  • *X*****[...] Now that last part from the OR was the important part. The 1 OR 1 = 1 bit, the scenario where you can bring $20 OR return the book. So let's do a logical comparison to see what is a good idea. Doing both is kinda dumb. That's where X comes in- the X is for eXclusive. Seriously. It gets rid of a special case in OR where OR is true if A or B is 1. X says, don't use the 1 / 1 = true option, I want exclusively 1 value true. That is where a lot of people get confused, OR is at least one bit is true, XOR says exactly 1 bit is true. So now 1 XOR 1 is(=) 0.

That's it from those you get all the options.

  • NOT - it's not really a comparator, just inverts 1 input, but usually lumped in
  • AND - both compared values are true, nothing else.
  • NAND - inverted AND, so if both are true, then is false, everything else is true- (0,1)(0,0)(1,).
  • OR - At least one is true (1,0)(1,1)(0,1) = True, only (0,0) is false
  • NOR- Inverts OR, so from above, (1,0)(1,1)(0,1) = False, and (0,0) is true.
  • XOR - Gets rid of the special (1,1) = true case in OR. The X only works on OR, Says now exactly 1 is true.

    • XNOR - This one will make your head hurt. But just do it stepwise with a truth table. We start with OR, and remove (1,1) = true, so when the bits match = false, and when they are different its true (1,0)(0,1). then invert it. So now, it's false when the bits differ (1,0)(0,1). But it's true when they match. (1,1) and (0,0), So this is an AND gate, and an inverted AND (NAND) gate joined by an OR/XOR gate. (The X is irrelevant, as you cant have both bits simultaneously be 1,1 and 0,0 unless we're talking quantum stuff.


Related Topics



Leave a reply



Submit