Informações

Autores Arthur van Stratum
Prazo de entrega Sem prazo
Limite de submissão No limitation
Category tags s2, level2, category_bit

Etiquetas

Entrar

[S2] Bitwise operation: resetting the highest order bit

In this exercise, we will work with operation on bits. When we speak about the position of a bit, index 0 corresponds to lowest order bit, 1 to the second-lowest order bit, ...

In C source code, you can write a number in binary (base 2) by prefixing it via 0b., e.g. 0b11010 = 26.

This exercise will introduce some non-standard data types which guarantee that the variable has a fixed number of bits. Indeed, on some machines, a int could use 2, 4 or 8 bytes. Hence, if we want to perform bitwise operations, we have to know first on how many bits we are working.

For this, C introduces a new class of variable types :

  • int8_t (signed integer of 8 bits)
  • uint8_t (unsigned integer of 8 bits)
  • uint16_t (unsigned integer of 16 bits)

You can mix uint or int with bit-lengths 8, 16, 32 and 64). These types are defined in <stdint.h>


Write the body of a function reset_highestorder_bit, which sets to 0 the highest bit set to 1 it founds, and does nothing if there's no bit set to 1.

For example, with bytes, reset_highestorder_bit(0b000111010101010) would return 0b000011010101010 and reset_highestorder_bit(0b100111010101010) would return 0b000111010101010

To write this function, you first need to determine the position of the highest order bit that is set to 1 and the reset its value.

#include <stdint.h>
uint32_t reset_highestorder_strong_bit(uint32_t x) {