# VHDL: OR-ing bits of a vector together

I want to OR the bits of a vector together. So say I have a vector called example(23 downto 0) and I want to OR all the bits into another vector, is there any way to do this that does not involve going example(0) or example(1) or ...example(23)?

• Could you simply compare to zero instead? That would have the same effect. – David Oct 19 '13 at 20:50
• To expand on David's comment (using a 32 bit vector): or_result <= '0' when input=X"00000000" else '1'; Change the number of zeros to match the length of the vector in question. – user60166 Dec 10 '14 at 8:18
• Logic reduction is available in vhdl 2008, see stackoverflow.com/questions/20296276/… – Moberg Feb 28 '17 at 12:47
• Also you can use more general way: result <= '0' when (example=(example'range=>'0')) else '1';  – Miguel Risco Apr 16 '17 at 5:06

or_reduce is what you want, and it is available in std_logic_misc. Supported by both A and X for FPGAs.

• and_reduce is the other useful one. – Aaron D. Marasco Oct 20 '13 at 12:26

Verilog has a convenient "reduction operator" that does exactly what you're asking for: |example[23:0] gives the result of OR'ing all the bits of the example vector.

Unfortunately VHDL doesn't have this operator. According to the comp.lang.vhdl FAQ, though

There is no predefined VHDL operator to perform a reduction operation on all bits of vector (e.g., to "or" all bits of a vector). However, the reduction operators can be easily implemented:

[skipping an example that doesn't handle 'X' and 'Z' values]

    function or_reduce( V: std_logic_vector )
return std_ulogic is
variable result: std_ulogic;
begin
for i in V'range loop
if i = V'left then
result := V(i);
else
result := result OR V(i);
end if;
exit when result = '1';
end loop;
return result;
end or_reduce;
...
b <= or_reduce( b_vec );

• Whoever downvoted, care to explain why? – The Photon Oct 21 '13 at 2:03
• Is this synthesizable? – Johannes Schaub - litb Aug 3 '17 at 17:06
• @JohannesSchaub-litb, of course, it can be synthesized to a really big OR gate (or a tree of smaller ones). Possibly the version in the standard library (in Aaron D. Marasco's answer) will be better optimized than something generated on the fly. – The Photon Aug 3 '17 at 20:17
• VHDL-2008 does have unary reduction operators. The FAQ is outdated. Furthermore, the presented function is of questionable synthesizability because of the early exit which some tools may choke on and isn't necessary other than as a micro-optimization for simulation. – KevinThibedeau Jan 1 at 3:08