Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How is a Hex Value manipulated bitwise?

I have a very basic understanding of bitwise operators. I am at a loss to understand how the value is assigned however. If someone can point me in the right direction I would be very grateful.

My Hex Address: 0xE0074000

The Decimal value: 3758571520

The Binary Value: 11100000000001110100000000000000

I am trying to program a simple Micro Controller and use the Register access Class in the Microsoft .Net Micro Framework to make the Controller do what I want it to do.

Register T2IR = new Register(0xE0074000);
T2IR.Write(1 << 22);

In my above example, how are the bits in the Binary representation moved? I don’t understand how the management of bits is assigned to the address in Binary form.

If someone can point me in the right direction I would be very greatfull.

like image 346
Rusty Nail Avatar asked Jun 10 '12 22:06

Rusty Nail


People also ask

Which bit manipulation is used to set a specific bit?

We can use the bitwise or operator to set a particular bit in a number to 1. We can use a combination of the and operator and an inverse mask to set any bit in a number to 0. The exclusive or operator is most often used to flip bits to their opposite state.

What is bitwise manipulation used for?

Bit manipulation is the act of algorithmically manipulating bits or other pieces of data shorter than a word. Computer programming tasks that require bit manipulation include low-level device control, error detection and correction algorithms, data compression, encryption algorithms, and optimization.

What is a bit in hex?

Each hexadecimal digit represents four bits (binary digits), also known as a nibble (or nybble). For example, an 8-bit byte can have values ranging from 00000000 to 11111111 in binary form, which can be conveniently represented as 00 to FF in hexadecimal.

What is logical bit manipulation?

Bit manipulation is the process of applying logical operations on a sequence of bits to achieve a required result.


1 Answers

Refer to @BerggreenDK's answer for what a shift is. Here's some info about what it's like in hex (same thing, just different representation):

Shifting is a very simple concept to understand. The register is of a fixed size, and whatever bits that won't fit falls off the end. So, take this example:

int num = 0xffff << 16;

Your variable in hex would now be 0xffff0000. Note how the the right end is filled with zeros. Now, let's shift it again.

num = num << 8;
num = num >> 8;

num is now 0x00ff0000. You don't get your old bits back. The same applies to right shifts as well.

Trick: Left shifting by 1 is like multiplying the number by 2, and right shifting by 1 is like integer dividing everything by 2.

like image 150
cyanic Avatar answered Sep 21 '22 12:09

cyanic